Ceuta was conquered by the Portuguese under king John I in 415. It was besieged by in 1418 by a sultan of the Marinid dynasty who controlled Morocco, but was defeated by Philip II, the king of Spain who also acceded by the Portuguese throne. In 1688 Alfonso VI of Portugal ceded Ceuta to Carlos II of Spain in the Treaty of Lisbon.