The Hafsid dynasty was the first of the three successor dynasties to emerge from the collapse of the Almohad Caliphate in the mid 13th century. Unlike the Zayyanids, who had merely been regional governors in Tlemcen under the Almohads, and the Marinids, who had never been anything more than an enemy to the Almohads, the Hafsids had been part of the Almohad project from its earliest days as a religious revivalist movement. Unlike the Marinids and Zayyanids, the Hafsids emerged from the same Masmuda Amazigh background as Ibn Tumart and the Almohads. In fact, the dynasty could trace its ancestry to one of Ibn Tumart’s closest companions, Omar Abu Hafs al-Hentati, so it held a prominent position in the caliphate throughout its history.
Like the Zayyanids, the Hafsids first assumed power in their territory (roughly modern Tunisia, with a bit of eastern Algeria and some of western Libya added) as Almohad governors, in this case of Ifriqiya (again, essentially modern Tunisia), starting in the early 13th century. But unlike the Zayyanids or the Marinids, the Hafsids’ long-standing prominence in the caliphate caused them to view their kingdom as the true and righteous successor to the Almohad legacy rather than just a new polity emerging from the wreckage of the old. Thus when the Almohad Caliph Idris al-Maʾmun cast off the traditional Almohad interpretation of Islam in favor of a more strictly Sunni orthodox one in the late 1220s, that’s when the Hafsid governor Abu Zakariya Yahya declared his independence from the caliphate, in 1229. In doing so, Abu Zakariya Yahya portrayed himself as the upholder of the true Almohad doctrine against Idris al-Maʾmun’s corruption. Abu Zakariya Yahya began referring to himself as amir al-muʾminin, or “commander of the faithful,” a title traditionally reserved for caliphs. Presumably he was claiming the caliphate for himself in doing so.
In 1242 Abu Zakariya Yahya captured Tlemcen, but when he couldn’t find anyone who could hold on to the city in his name, he opted to restore the Zayyanid ruler Yaghmurasen ibn Zyan to power under the condition that Yaghmurasen submit to Hafsid suzerainty. Since the Hafsids couldn’t really project authority that far west anyway, Yaghmurasen agreed to these terms and was restored to power. And when Abu Zakariya Yahya died in 1249 his successor, Muhammad I al-Mustansir (d. 1277) preferred to consolidate his control over Ifriqiya rather than trying to expand his domains. Interestingly, al-Mustansir took the title of “caliph,” again showing that the Hafsids considered themselves heirs to the Almohads. He expanded Ifriqiya’s commercial relationships with the Italian states of Genoa, Pisa, and Venice, as well as the kingdom of Aragon, and Ifriqiya prospered as a result. During his lengthy and notably stable reign, al-Mustansir had the privilege of watching the Eighth Crusade degenerate into a complete catastrophe right outside the walls of Tunis. Nevertheless, al-Mustansir eventually had to buy the crusaders’ departure through a number of trade concessions and a reparations payment.
The stability that marked al-Mustansir’s reign unfortunately died with him. Over the next seven years the Hafsids churned through five rulers, thanks mostly to internal dissension exploited by the Aragonese, who wanted to expand their influence in Ifriqiya and their control over its commercial activity. Abu Hafs Umar I took power in 1284 and ruled for the next 11 years, but his reign was marked by division in the Hafsid kingdom as its western half, centered around the cities of Béjaïa and Constantine, was held by rival claimants to the throne as a virtually independent polity. Badly weakened, Abu Hafs agreed to become the vassal of Aragonese King Peter III.
The period of a divided Hafsid polity and of extreme Aragonese control over Ifriqiya came to an end under Abu Bakr II, who ruled the Hafsid kingdom from 1318 to 1346 and was the first Hafsid whose reign extended more than 10 years since al-Mustansir. He first established himself as the ruler of the western part of the kingdom, at Béjaïa, before moving east and toppling Abu Yahya Zakariya ibn al-Lihyani in Tunis. Abu Bakr didn’t cut ties with Aragon entirely but he did stop making tribute payments, and while the Aragonese attempted to reassert their power in Tunis by backing rivals who were seeking to overthrow Abu Bakr, none of these efforts panned out.
Abu Bakr II was ruling the Hafsid kingdom when Ibn Battuta crossed its border in 1325, but despite the length of his reign he never achieved the same authority over his domains as al-Mustansir had possessed half a century earlier. The 40 year period of instability and Aragonese meddling between al-Mustansir’s death and Abu Bakr’s rise to power caused local dynasties to arise in most of the kingdom’s major cities and Arab tribes to assert control over whole regions of central and southern Ifriqiya while frequently extorting payments from those cities. Tripoli, which had been Ibn al-Lihyani’s home base before he seized control of Tunis, now became independent altogether.
In addition to the breakdown in territorial control, the Hafsids also suffered a loss of internal cohesion as the system they’d built up around the idea that they were the heirs to the Almohads began to break down. The early Hafsids had maintained the Almohad name in reference to their military, with “Almohad” sheikhs–religious leaders of the Almohad movement–serving as the leaders of the tribal groups that made up the core of the Hafsid army and the basis for the dynasty’s political legitimacy. But the increasing influence of Aragon diminished and eventually broke the power of these Almohad remnants, and then Abu Bakr’s accession broke the power of the Aragonese, and so the Hafsid kingdom found itself very much in a transitional period during his reign.
First the Zayyanids, then the Marinids tried to take advantage of this decline in Hafsid power–the Zayyanids were at war with the Hafsids during Ibn Battuta’s journey, and the Marinids swept across North Africa and conquered Tunis twice in the 1340s and 1350s. But they couldn’t hold the city either time and the Hafsids eventually resumed control. Under Abu al-Abbas Ahmad II, who ruled from 1370 through 1394, the Hafsids regained their authority by bringing those runaway Arab tribes to heel and winning back the support of the kingdom’s cities in the process. They also increasingly turned away from Almohad religious doctrine to orthodox Sunnism, specifically of the Maliki school of law, as the religious backbone of their rule. Abu Faris Abd al-Aziz II, who reigned from 1394 through 1434, continued this process and further consolidated Hafsid authority. He also brought Tripoli back under Hafsid control.
While commercial ties between the Hafsids and various European states remained strong, the Hafsids also increasingly looked to piracy as a way to bring in revenue. This led to conflict with European Christian kingdoms on some occasions, particularly against Aragon, and eventually Tunis would become one of the chief ports of call for the Barbary corsairs.
Abu Amr Uthman (1435-1488) was the third and final of the three post-Marinid Hafsid rulers to rebuild the Hafsid kingdom, and his reign saw its high water mark. He led his forces west across North Africa, subjugating the Zayyanids and the Wattasids, the successors to the Marinids in Morocco, in the 1460s. But his death saw a return of the old dynastic squabbles, the resurgence of uncontrolled Arab tribes in the Tunisian countryside, and the reintroduction of Aragonese–well, Spanish now, meddling in Hafsid affairs. The Ottomans, particularly alarmed by the latter development, conquered the Hafsid kingdom three times in the 1500s. The first two didn’t take, but after the last, in 1574, they carted the Hafsid ruler Muhammad VI off to Constantinople and executed him. Tunisia would remain part of the Ottoman Empire, albeit often a very autonomous part, until it became a French protectorate in 1881.