Apple CEO Tim Cook attends a state dinner at the White House in April. (Jabin Botsford/The Washington Post)

Apple chief executive Tim Cook has been one of President Trump’s staunchest critics in Silicon Valley, opposing the White House on immigration, climate change and more.

But the 57-year-old tech leader has also become one of the technology industry’s savviest political operators — a behind-the-scenes Trump whisperer, able to shape some of the administration’s economic policies in ways that benefit Apple and some of its tech peers.

Those efforts seemed to pay off Monday, after Trump unveiled tariffs on roughly $200 billion in goods imported from China, the latest salvo in the trade war Washington is waging against Beijing. The initial list of imports the White House had threatened to penalize included some of Apple’s best-known products, the company said earlier this month, such as its recently updated Apple Watch smartwatch, HomePod home assistant and AirPods wireless headphones (but not the iPhone). On Monday evening, though, those products were spared. Thousands of other imports weren’t so lucky, and Americans could soon be paying more for such things as refrigerators and toys.

For months, Cook had lobbied Trump personally against adopting such tariffs, according to people familiar with the Apple executive’s efforts and Cook’s public comments. Apple warned the Trump administration in a regulatory filing this month that the government’s proposal could result in higher prices on some of the world’s most popular consumer electronics. “Our concern with these tariffs is that the U.S. will be hardest hit, and that will result in lower U.S. growth and competitiveness and higher prices for U.S. consumers,” Apple said. Apple added that the policy would have amounted to a “tax on U.S. consumers,” who rely on its products, like the Apple Watch, particularly to monitor their heart health.

Asked on Tuesday about the Trump administration’s decision, Cook told ABC’s “Good Morning America” that he thinks the White House “looked at this and said that it’s not really great for the United States to put a tariff on those type of products.”

The Trump administration has acknowledged Cook’s effect on its thinking. “We’ve spoken to Mr. Tim Cook many times,” said Larry Kudlow, the White House economic adviser, during an appearance a day earlier in New York. “He’s given us some good advice.”

A spokesman for the White House did not respond to a request for comment. A spokesman for Apple declined to comment.

A person briefed on the planning, who spoke on condition of anonymity to discuss internal White House matters, said there was a concerted decision to exempt a group of consumer products late in the discussions, with the understanding that they could be added to the final set of tariffs Trump has threatened to impose. The decision was not made as a way to intentionally carve out Apple, as many other companies benefited.

Cook’s appeals to Trump on trade appeared to intensify this spring. The Apple executive scored a rare invite to the White House state dinner to honor the visiting French president in April, and a day later, Cook privately huddled with Trump at the White House to express his view that “tariffs were not the right approach” with China, Cook later told Bloomberg Television.

The outreach eventually spared Apple from at least one headache: The company’s iPhone dodged steep new fees entering the country in Trump’s earlier round of tariffs. But Trump still ratcheted up his attacks on China in the weeks to follow and over the summer proposed two more rounds of tariffs covering a host of goods, including Apple-made consumer electronics.

That touched off another all-out lobbying blitz on Apple’s part. Cook paid a visit to Trump National Golf Course in Bedminster, N.J., in early August to dine with the president and again talk tariffs, according to two people familiar with the meeting who were not authorized to speak on the record.

The president initially bristled at Apple’s comments about the potential effect of tariffs, proposing that Apple could fix the problem by making more of its products in the United States. Yet he appeared to acknowledge in a tweet on Sept. 8 that prices on Apple’s products “may increase because of the massive Tariffs we may be imposing on China.” But the Trump administration on Monday ultimately decided against imposing tariffs on key technologies like its AirPods, HomePod and Apple Watch, a new version of which Apple announced last week.

“The iPhone is assembled in China, but the parts come from everywhere,” Cook told ABC on Tuesday in response to a question about the tariffs. “. . . The glass comes from Kentucky, there are chips that come from the U.S., and of course the research and development is all done in the United States.”

Cook’s successful outreach reflects a dramatic departure from Apple’s tumultuous early relationship with Trump, even before he entered the White House. Frustrated with Trump’s comments on the campaign trail about immigrants, women and minorities, Cook withdrew Apple’s support from the 2016 Republican convention that nominated Trump for president. Then, Cook helped raised money for Trump’s Democratic foe, Hillary Clinton. Trump, meanwhile, attacked Apple on the campaign trail, at one point calling for a boycott of its products.

In the weeks after Trump’s inauguration in January 2017, Cook emerged as a fierce critic of some of the new administration’s signature policies, including its decision to withdraw from a major international climate agreement and end a program that protected some young immigrants from deportation. Cook hasn’t let up on the criticism, even describing the White House’s policy of separating families who arrive in the United States illegally as “inhumane” this June.

Even while sharply criticizing the president, however, Cook has seized on some opportunities to praise the Trump administration, including the tax cuts Trump signed into law last year. Cook also has sought to build bridges with Trump and his top aides to advance Apple’s agenda on key business issues. Cook dined with Jared Kushner and Ivanka Trump at Tosca, an upscale Italian restaurant in Washington, in early 2017, according to news reports at the time. He joined his peers from Facebook, Google and Intel months later to huddle with Trump and his top aides to talk about ways to modernize government. In March, Apple invited Treasury Secretary Steven Mnuchin for a visit to its California headquarters, drawing praise from the Trump confidant over the iPhone giant’s pledge to invest more in the United States. Apple has committed to returning much of the $252 billion in cash it held abroad.

Cook hasn’t been alone in trying to lobby the White House — or in urging the Trump administration to stand down on tariffs targeting consumer electronics. The Information Technology Industry Council, a trade group that includes Apple, Amazon, Facebook and Google, similarly had lobbied the Trump administration, as had individual device-makers such as Fitbit. The company, which makes a popular wristband fitness-tracker, told the U.S. Trade Representative in September that the Trump administration’s original proposal could cause “significant and unavoidable economic harm to American companies.” It’s been spared, but many in the tech industry fear a worsening trade war.

“There’s concern around the world this is an escalating trade war without a solution that's going to affect the world economy,” said Gary Shapiro, the leader of the Consumer Technology Association.

For Apple, it could mean headaches still to come. China is taking steps to retaliate against Trump’s new tariffs, a major threat for the iPhone giant, which attributes about one-fifth of its $229 billion in annual revenue to China. Trump has signaled he would impose tariffs on all other Chinese products if tensions continue to escalate. That means Apple products, including the iPhone, eventually could become caught in the middle.

Cook told ABC on Tuesday that he is “optimistic that the two countries will sort this out, and life will go on.”

Analysts, meanwhile, said Apple had notched a major victory, at least for the moment.

“Some of the most powerful companies in the United States, some of the very same companies that President Trump needs to succeed, are caught in the crosshairs,” said Tom Forte, an analyst tracking consumer technology companies at D.A. Davidson. “Clearly Tim Cook has been effective in his developing a relationship with the current administration, and this is probably one of the best examples of the fruits of that labor.”