Sir David McNee, who has died aged 94, was Britain’s most senior police officer at a time of racial, political and industrial turmoil in London – and one of the last of the old-style, non-graduate officers to lead the Metropolitan force. It was on his watch, as commissioner from 1977 until 1982, that the Brixton and Southall riots in London, the siege of the Iranian embassy and the break-in at the Queen’s bedroom at Buckingham Palace took place.

McNee had the difficult task of succeeding the charismatic Sir Robert Mark as commissioner and never appeared completely at ease in the post. Although he enjoyed the support of Margaret Thatcher, the prime minister, he felt that the police were often blamed unfairly for the failings of politicians.

The only son of John McNee, a railwayman, who drove the Royal Scot, and his wife, Mary (nee Blackstock), who had met in an evangelical mission hall, McNee had a profoundly religious upbringing. Although he was less ostentatious about his beliefs than his contemporary and friend James “God’s Copper” Anderton, the former chief constable of Greater Manchester, he saw the police service as akin to a Christian duty.

Growing up in a Glasgow tenement, McNee attended Dundas Vale primary and Woodside secondary schools. He was an active member of the local Boys’ Brigade and played the trombone in their band; later he also regularly sang as a tenor in church choirs. After leaving school at 15, a decision he later regretted, he worked as a “dogsbody” at the Clydesdale Bank before being called up in 1943 and joining the Royal Navy. He served as a war-time telegraphist on a number of ships, including HMS Empire Mace, which was at the D-day landings in Normandy.

On demob, he joined Glasgow police in 1946 and established the “firm but fair” reputation that would accompany him throughout his career. After serving as a beat officer on some of the city’s rougher streets and in the special branch, a brief and successful spell at a senior level in Dunbartonshire police led to him being appointed to the top job in Glasgow in 1971 and, in 1975, as the first chief constable of the newly created Strathclyde police.

Following corruption scandals that damaged the Met in the 1970s, home secretaries saw the virtue in appointing a commissioner from outside London, and McNee was duly approached by the Labour home secretary, Merlyn Rees, to replace Mark in 1977. After consulting St Luke’s gospel about the need to use one’s “God-given” talents, he accepted the post, which he held for the next five and a half years. The Sun’s crime correspondent, George Hollingberry, claimed to have nicknamed him “the Hammer” after a Scottish reporter told him how he “hammered” the crooks of his home city.

An early test came in 1979 with a violent confrontation at a National Front rally in Southall, west London, opposed by the local Asian community and the Anti-Nazi League. In the ensuing riot, Blair Peach, a young schoolteacher and ANL activist, was killed in circumstances never fully explained but for which many held the police responsible. The Met were also heavily involved in the policing of the long-running union dispute at Grunwick’s photo-processing plant in north-west London, which led to mass arrests as pickets and officers clashed on an almost daily basis.

There was also growing tension between the police and parts of London’s black community, which finally exploded in the Brixton riots in 1981, in the wake of the police’s Operation Swamp, which many community leaders felt was a heavy-handed response to crime in the area. The riots led to the Scarman report on the policing of racially mixed areas and also to the routine issuing of protective riot gear to officers, around 400 of whom were injured in the clashes.

In his memoir, McNee’s Law (1983), McNee complained that the criticism of the police was undeserved and that they were the “political scapegoats” for government failures to address social problems. But he was applauded in 1980 for the handling of the siege of the Iranian embassy, which was taken over by six Iranian exiles demanding the release of prisoners in Iran. After a five-day standoff and the murder of a hostage, the SAS entered the building and saved all but one of the remaining 25 hostages. Five of the six terrorists were killed.

A keen proponent of the “bobby on the beat”, McNee oversaw a popular increase in the number of uniformed officers on patrol but his “stop and search” policy was less successful and further aggravated community relations. His time in office was also dogged by Operation Countryman, a lengthy and inconclusive investigation into alleged corruption, based on what McNee dismissed as “tittle-tattle”; it resulted in a handful of unsuccessful prosecutions of officers, although a much larger number took early retirement.

But it was the break-in at Buckingham Palace in 1982 by the enigmatic figure of Michael Fagan that was to cause McNee most distress. Amazingly, the barefooted Fagan, who had psychiatric problems, was able to scale the railings of the palace, climb unchallenged through a window and stroll into the Queen’s bedroom. It took 10 minutes for the police to arrive. It was “the lowest point of my entire career”, McNee said later. This embarrassment, coupled with the revelation that the Queen’s personal protection officer, Commander Michael Trestrail, was having a relationship with a sex worker, led to calls for McNee to resign. He resisted this but felt badly let down by a lack of support from the home secretary, Willie Whitelaw.

A staunch royalist, knighted in 1978, McNee campaigned unsuccessfully behind the scenes for his force to be renamed the Royal Metropolitan police and was gratified that Princess Margaret came to his leaving party in 1982. Of conventionally conservative views, he supported the return of the death penalty, arguing that the “shadow of Tyburn” made criminals think twice about going armed.

In his retirement McNee took on directorships with his first employer, the Clydesdale Bank, and with Trusthouse Forte, Scottish Express Newspapers and Integrated Security Services; he also acted as an adviser to British Airways. He chaired his fellow evangelical Billy Graham’s Mission Scotland crusade in 1991.

He was married to Isabel Hopkins from 1952 until her death in 1997 and they had a daughter, Heather. In 2002, he married Lilian Campbell, the widow of a close friend. He is survived by Lilian, Heather, two stepchildren, Laura and Finlay, and two grandchildren, Bret and Stephanie.

• David Blackstock McNee, police officer, born 23 March 1925; died 26 April 2019