onverts in the towns and among slaves and Berber farmers.
Islamic expansion began in the 7th century. In 670 AD, the first Islamic conquest of the North African coastal plain took place under Uqba ibn Nafi, a general serving under the Umayyads of Damascus. After the outbreak of the Great Berber Revolt in 739, the region's Berber population asserted its independence, forming states and kingdoms such as the Miknasa of Sijilmasa and the Barghawata. Under Idris ibn Abdallah, who was appointed by the Awraba Berbers of Volubilis to be their representative, the country soon cut ties and broke away from the control of the distant Abbasid caliphs in Baghdad and the Umayyad rule in Al-Andalus. The Idrisids established Fes as their capital and Morocco became a centre of Muslim learning and a major regional power.
Morocco would reach its height under a series of Berber dynasties that replaced the Idrisids after the 11th century. From the 13th century onwards the country saw a massive migration of Banu Hilal Arab tribes. Their arrival was to have a critical effect on the nation: due to them nomadism returned, urban civilization fell and the country's inhabitants were quickly becoming Arabized. The Almoravids, the Almohads, the Marinids, the Wattasids and finally the Saadi dynasty would see Morocco rule most of Northwest Africa, as well as large sections of Islamic Iberia, or Al-Andalus. Following the Reconquista of the Iberian Peninsula, large numbers of Muslims and Jews were forced to flee to Morocco.
After the Saadi, the Alaouite Dynasty eventually gained control. Morocco was facing aggression from Spain and the Ottoman Empire that was sweeping westward. The Alaouites succeeded in stabilizing their position, and while the kingdom was smaller than previous ones in the region, it remained quite wealthy. In 1684, they annexed Tangier. The organization of the kingdom developed under Ismail Ibn Sharif (1672–1727), who, against the opposition of local tribes began to create a unified state. According to Elizabeth Allo Isichei, "In 1520, there was a famine in Morocco so terrible that for a long time other events were dated by it. It has been suggested that the population of Morocco fell from 5 to under 3 million between the early sixteenth and nineteenth centuries."
Morocco was the first nation to recognize the fledgling United States as an independent nation in 1777. In the beginning of the American Revolution, American merchant ships were subject to attack by the Barbary Pirates while sailing the Atlantic Ocean. On 20 December 1777, Morocco's Sultan Mohammed III declared that the American merchant ships would be under the protection of the sultanate and could thus enjoy safe passage. The Moroccan-American Treaty of Friendship stands as the U.S.'s oldest non-broken friendship treaty.
Successful Portuguese efforts to invade and control the Atlantic coast in the 15th century did not profoundly affect the Mediterranean heart of Morocco. After the Napoleonic Wars, Egypt and the North African Maghreb became increasingly ungovernable from Istanbul, the resort of pirates under local beys, and as Europe industrialized, an increasingly prized potential for colonization. The Maghreb had far greater proven wealth than the unknown rest of Africa and a location of strategic importance affecting the exit from the Mediterranean. For the first time, Morocco became a state of some interest in itself to the European powers.
France showed a strong interest in Morocco as early as 1830. Recognition by the United Kingdom in 1904 of France's sphere of influence in Morocco provoked a reaction from the German Empire; the crisis of June 1905 was resolved at the Algeciras Conference in Spain in 1906, which formalized France's "special position" and entrusted policing of Morocco jointly to France and Spain. The Agadir Crisis provoked by the Germans, increased tensions between European powers. The Treaty of Fez (signed on March 30, 1912) made Morocco a protectorate of France. By the same treaty, Spain assumed the role of protecting power over the northern and southe