A Saudi Air Force officer who went on a deadly gun rampage at a US naval base in December 2019 worked with al-Qaeda in the Arabian Peninsula (AQAP) to plan the attack, US officials say.
The gunman's phone revealed the terror link, the FBI and Department of Justice said.
The Pensacola, Florida, attack killed three US sailors and injured eight.
Prosecutors criticised Apple for refusing to unlock the gunman's phones, which authorities took months to hack.
The attack led to the expulsion of 21 Saudi military pupils who had been studying at the base with the attacker.
According to the FBI, the gunman, who was killed by authorities after shooting 11 people, had been radicalised before arriving Florida for a three-year aviation course the US navy hosts for allied foreign servicemen.
Records revealed that he had been in active contact with AQAP, an off-shoot of al-Qaeda based in Yemen, up to the shooting, including talking about plans to carry out a "special operation" for them.
The group's leader, Qasim al-Raymi, was confirmed in February to have been killed during a US operation. AQAP had released an audio message earlier that month with Raymi's voice, in which he said the group was behind the naval base shooting.
FBI Director Christopher Wray said that gunman was "more than inspired" by AQAP, sharing tactics and "co-ordinating" with the group on plans for attack.
The attack strained US-Saudi relations and led the defence department to pause all international military training in the US until further vetting measures could be put in place.
'Apple did nothing to help'
In disclosing details of the terror investigation on Monday, the Trump administration criticised Apple for refusing to help unlock the gunman's phones, which he had attempted to destroy before he was shot dead following the attack.
Hacking into his two phones four months after the attack diverted attention from other important work and gave conspirators time to evade justice, Mr Wray and Attorney General William Barr said.
"Apple has made a business and marketing decision to allow only the user to unlock their phones, no matter the circumstances," said Mr Barr, adding that the company had ignored a personal request from President Donald Trump.
"Apple's decision has dangerous consequences for public safety and the national security, and is in my judgement, unacceptable," he added, calling on new laws to mandate Apple's future co-operation.
Mr Wray told reporters that the phones had already yielded valuable intelligence, which led directly to a US counter-terror operation in Yemen.
Apple has denied FBI requests to unlock its iPhone devices in several high profile mass shooting cases, citing larger concerns over privacy.
Who was the attacker?
Mohammed Alshamrani had been speaking regularly with a contact with AQAP and was "certainly more than just inspired" by the terror group, Mr Wray said when asked if the attacker had been "directed" from abroad.
He was frequently "sharing plans and tactics with them", including video he made of the campus, said Mr Wray.
He was "co-ordinating with them and providing them an opportunity to take credit," he said, adding that the group released Alshamrani's will after his death.
He was radicalised in 2015, officials said, and his communications with the terror group "continued right up until the end, the very night before he started shooting".
The expelled Saudi students were found to have had jihadist material and indecent images of children in their possession, investigators have said, but none were accused of aiding the killer.
US Attorney General William Barr vowed that Saudi Arabia would conduct a review to determine if any soldiers should face military discipline or criminal charges in the US. The investigation continues.
How have the Saudis responded?
In a statement on Monday, the Saudi embassy in Washington DC said it "reiterates its sincerest condolences to the American people".
"Saudi Arabia will continue to use every means at our disposal to counter the men, mindset and money of terrorism that enables AQAP and others to recruit followers and threaten communities, or even entire nations," it said.
It added: "But we will never let the terrorists win, or allow their acts of hatred to divide us. The US-Saudi partnership is one of the primary pillars of the global effort to dismantle and defeat terrorist networks such as AQAP.