During his father's reign, neighbouring Ottoman Algeria was invaded by France in 1830, and Muhammad commanded the Moroccan army which was defeated by the French at the Battle of Isly in August 1844 during the Franco-Moroccan War.
After the defeat, with his father's permission, Mawlay Muhammad used his capacity as army chief to launch a series of significant military reforms in 1845.
The new units were infantry, as ooposed to the old-style mounted cavalry, enabling the Moroccans to replicate the tactics that had produced the concentrated firepower the French had displayed at Isly.
[5] The new sultan hardly looked the part of a modernizer with his flowing white robes and mane of curly black hair, but he was in fact immersed in the language of reform and fully aware of the technological transformations taking place elsewhere in the region.
He had reportedly perused military manuals translated from Turkish and European languages into Arabic, and he had a lively curiosity about technical innovations.
The Treaty of Wad Ras signed in April 1860 expanded the enclaves, but more worrisomely imposed a large indemnity payment on Morocco of 100 million francs, twenty times the government's budget.
After the disappointment of defeat and the crushing financial burden of the Spanish treaty, Muhammad IV gradually retired into passivity, dedicating himself to scholarly and intellectual interests in mathematics, geometry, astronomy, poetry and music, and leaving political affairs to be handled by his palace slave and effective vizier, Si Moussa.As by the Treaty of Tangier in 1863, half of the customs duties of all Moroccan ports were designated to pay the Spanish debt, the Alawite sultan's government (the Makhzen) was faced with a critical financial situation, and launched the process of "qaidization".
[7] Traditionally, the Makhzen had an understanding with the semi-autonomous rural tribes, whereby the tribal leaders agreed to hand over a portion of the taxes they collected and to supply tribesmen to the sultan's army in times of war, but otherwise were left to manage their own affairs.