structural:scholar_edition, structural:summarization, quality:correction, quality:corruption, structural:reformatting
Rivalry with Córdoba

Apart from the Abbasids, al-Mahdi faced a major Muslim rival closer to Ifriqiya, in the form of the Umayyads of Córdoba, rulers of al-Andalus (Islamic Spain). However, the confrontation between the two major Islamic powers of the western Mediterranean took place mostly via propaganda and proxies, rather than direct conflict. At the time of al-Mahdi's accession, the Umayyad emirate was plagued by internal dissension, notably the uprising of Ibn Hafsun, who in his conflict with Emir Abdallah (r. 888–912) pledged his allegiance to al-Mahdi. Al-Mahdi sent robes of honour and two Isma'ili da'i s to the rebel leader, in whose territories the Friday sermon was read in the name of the Fatimid caliph.

Al-Mahdi's campaigns in the western Maghreb, in what is now Morocco, were in part meant to "spread fear on the threshold of the Iberian Peninsula", according to historian Farhat Dachraoui, but the restiveness of the Berber tribes limited the ability of the Fatimid ruler to project power beyond Tahert and seriously contemplate an invasion of al-Andalus. Nevertheless, the Fatimid threat was taken seriously by the Umayyad emir Abd al-Rahman III (r. 912–961), who reinforced his fleet and established patrols along his southern coasts as one of his first measures upon his accession. During the first two decades of his rule, Abd al-Rahman was occupied with suppressing revolts, most notably that of Ibn Hafsun; but as his power grew, in 927 an Umayyad fleet captured Melilla, establishing it as a military base in the Moroccan coast, followed by Ceuta in 931. Abd al-Rahman also entered into an alliance with Ibn Khazar, recognizing him as the 'paramount emir of the Zenata' and sending him frequent gifts, in exchange for the Berber leader's recognizing Umayyad suzerainty. Finally, once he had secured his own position in al-Andalus, in 929 Abd al-Rahman III claimed the title of caliph for himself, establishing the Caliphate of Córdoba, in a direct challenge to al-Mahdi's pretensions, both temporal and religious.