The greatest threat to Almohad rule in Tunisia was the Banu Ghaniya, relatives of the Almoravids, who from their base in Mallorca tried to restore Almoravid rule over the Maghreb. Around 1200 they succeeded in extending their rule over the whole of Tunisia until they were crushed by Almohad troops in 1207. After this success, the Almohads installed Walid Abu Hafs as the governor of Tunisia. Tunisia remained part of the Almohad state, until 1230 when the son of Abu Hafs declared himself independent. During the reign of the Hafsid dynasty, fruitful commercial relationships were established with several Christian Mediterranean states. In the late 16th century the coast became a pirate stronghold.
In the last years of the Hafsid dynasty, Spain seized many of the coastal cities, but these were recovered by the Ottoman Empire.
Initially under Turkish rule from Algiers, soon the Ottoman Porte appointed directly for Tunis a governor called the Pasha supported by janissary forces. Before long, however, Tunisia became in effect an autonomous province, under the local Bey. Under its Turkish governors, the Beys, Tunisia attained virtual independence. The Hussein dynasty of Beys, established in 1705, lasted until 19570. This evolution of status was from time to time challenged without success by Algiers. During this era the governing councils controlling Tunisia remained largely composed of a foreign elite who continued to conduct state business in the Turkish language.
Attacks on European shipping were made by corsairs, primarily from Algiers, but also from Tunis and Tripoli, yet after a long period of declining raids the growing power of the European states finally forced its termination. Under the Ottoman Empire, the boundaries of Tunisia contracted; it lost territory to the west (Constantine) and to the east (Tripoli).
In the 19th century, the rulers of Tunisia became aware of the ongoing efforts at political and social reform in the Ottoman capital. The Bey of Tunis then, by his own lights but informed by the Turkish example, attempted to effect a modernizing reform of institutions and the economy. Tunisian international debt grew unmanageable. This was the reason or pretext for French forces to establish a protectorate in 1881.
French Tunisia (1881–1956)
In 1869, Tunisia declared itself bankrupt and an international financial commission took control over its economy. In 1881, using the pretext of a Tunisian incursion into Algeria, the French invaded with an army of about 36,000 and forced the Bey to agree to the terms of the 1881 Treaty of Bardo (Al Qasr as Sa’id). With this treaty, Tunisia was officially made a French protectorate, over the objections of Italy. Under French colonization, European settlements in the country were actively encouraged; the number of French colonists grew from 34,000 in 1906 to 144,000 in 1945. In 1910 there were 105,000 Italians in Tunisia.