"The Portuguese rule lasted until 1662, when it was given to England's King Charles II as part of the dowry from the Portuguese Infanta Catherine of Braganza, becoming English Tangier.[6] The English gave the city a garrison and a charter which made it equal to English towns. The English planned to improve the harbour by building a mole. With an improved harbour the town would have played the same role that Gibraltar later played in British naval strategy. The mole cost £340,000 and reached 1,436 feet (438 m) long, before being blown up during the evacuation.[7]
An attempt of Sultan Moulay Ismail of Morocco to seize the town in 1679 was unsuccessful; but a crippling blockade by his Jaysh al-Rifi ultimately forced the English to withdraw. The English destroyed the town and its port facilities prior to their departure in 1684. Under Moulay Ismail the city was reconstructed to some extent, but it gradually declined until, by 1810, the population was no more than 5,000..."
https://en.wikipedia.org/wiki/Tangier
Basically, English Tangier was abandoned because (1) it was clear James would inherit the throne and so Tangier got caught up in Parliament's reluctance to subsidize a "Papist", and (2) Tangier was getting much more expensive to defend anyway against the Moroccans once the latter united under the Alaouites.
https://en.wikipedia.org/wiki/English_Tangier
AHC: Is there any way the English (and later the UK...) hold on to it to this day? Maybe when they acquire Gibraltar, they see Gibraltar and Tangier as strategic "bookends"? Or might they on the contrary think, "now that we have Gibraltar, we don't need Tangier" ? After all, unlike Gibraltar, Tangier was not a natural fortress. So it does seem likely that the English would abandon it or swap it sooner or later, [1] but that's why this is a challenge...
[1] Indeed, perhaps what needs explaining is why, given the expense, the English kept it for more than two decades. National pride may be one reason, but another was suggested by a friend: "maybe Charles liked having that garrison: a loyalist royalist standing army of tough seasoned soldiers, small and distant enough not to freak people out, but close enough to bring home fast in the event of another Civil War..."