John McCain, the longtime senator and 2008 GOP presidential nominee who refused to leave fellow prisoners of war behind to escape years of torture in Vietnam, and whose maverick streak as Arizona’s senior statesman at times frustrated fellow Republicans and confounded President Donald Trump, has died, his family confirmed in a statement Saturday.

He was 81 and had been diagnosed with brain cancer in July 2017.

His wife of 38 years, Cindy McCain, said the senator "passed the way he lived, on his own terms, surrounded by the people he loved, in the the place he loved best."

His daughter Meghan McCain said of her father: "He was a great fire who burned bright, and we lives in his light and warmth for so very long. We know that his flame lives on, in each of us."

McCain had decided to discontinue medical treatment for the glioblastoma, an aggressive form of brain cancer, his family announced on Aug. 24. He had surpassed expectations for his survival, but "the progress of disease and the inexorable advance of age render their verdict," the statement said.

McCain, the chairman of the Senate Armed Services Committee who was in his sixth term representing Arizona, had a reputation for sticking to his convictions even if it meant disagreeing with his fellow Republicans. He was a fierce critic of President Donald Trump, who in turn repeatedly insulted the ailing senator.

McCain's brain tumor was discovered last year after he underwent surgery to remove a blood clot. He recovered quickly from the surgery and in late July entered the Senate to a standing ovation.

On the Senate floor, he said that his service there had been the most important job he had held.

"And I am so grateful to the people of Arizona for the privilege – for the honor – of serving here and the opportunities it gives me to play a small role in the history of the country I love," he said.

In one of his most dramatic final votes, he gave a deciding thumbs-down to the GOP's last attempt to overturn "Obamacare," the health-care law approved under President Barack Obama. The early-morning vote earned McCain praise from Democrats and Trump's fury.

But he stuck with his party on the contentious GOP-backed tax reform bill in December though he was too ill to return to Washington for the final vote.

His disputes with Trump became especially nasty, and his allies had told the White House that the president was not welcome at McCain's funeral. That antipathy was on display in August when the president failed to mention McCain as he signed a defense bill intended to honor the senator.

"My deepest sympathies and respect go out to the family of Senator John McCain. Our hearts and prayers are with you!" Trump tweeted Saturday night.

After his cancer diagnosis, McCain spent a good deal of his time at his Arizona ranch, where he underwent physical therapy and received visitors, including his daughter, "The View" co-host Meghan McCain, and former Vice President Joe Biden, whose son Beau Biden died in 2015 after being diagnosed with the same brain cancer.

Among those who paid tribute to McCain were former presidents, fellow lawmakers and world leaders.

Senate Majority Leader Mitch McConnell said "the nation mourns the loss of a great American patriot" and called it "an understatement to say the Senate will not be the same" without him.

"In an era filled with cynicism about national unity and public service, John McCain's live shone as a bright example," McConnell said. "He showed us that boundless patriotism and self-sacrifice are not outdated concepts or cliches, but the building blocks of an extraordinary life."

McCain announced in a book excerpt released at the start of May that cancer forced him to acknowledge he would not be running for re-election.

John Sidney McCain III was born on Aug. 29, 1936, in the Panama Canal Zone to John Sidney and Roberta McCain. The son and grandson of Navy admirals and a graduate of the U.S. Naval Academy, he served in the U.S. Navy until 1981, when he retired with the rank of captain. He was elected to the House of Representatives from Arizona in 1982 and to the Senate in 1986.

During the Vietnam War, after his Navy bomber was shot down in 1967, he was held as a prisoner of war by the North Vietnamese for five and a half years. In 1968, after McCain's father had been named the commander of the U.S. forces in the Pacific, he was offered an early release but refused until those who had been at the prison longer than he were let go.

In 2000, he campaigned for the Republican presidential nomination in a bus he called the "Straight Talk Express," to capitalize on his reputation for independence. During the South Carolina primary, he and his family endured perhaps the ugliest incident of the race: a phony poll that was actually designed to spread the false story that he had fathered a black child during an affair. In fact, the McCains' daughter Bridget was adopted from an orphanage in Bangladesh. Other rumors pegged McCain as a traitor in Vietnam and his wife, Cindy, as a drug addict. (Cindy McCain has said she became addicted to Vicodin and Percocet in 1989 after back surgery for ruptured disks.)

The campaign of George W. Bush, his opponent and the ultimate winner, denied it was responsible.

McCain was the Republican nominee for president in 2008, running with then-Alaska Gov. Sarah Palin. She seemed a popular choice at first, but as time went on, opinions about her turned negative.

At a campaign meeting in Minnesota, McCain famously told a woman who said she could not trust Obama because he was an Arab that the then-Illinois senator was a decent family man and a citizen.

Trump, while campaigning in 2015, belittled McCain by saying he was not a war hero.

"He's a war hero because he was captured," Trump said. "I like people who weren't captured."

Politicians from both parties quickly rallied around McCain.

The week before McCain had said Trump had "fired up the crazies" in the Republican party with his inflammatory talk about illegal immigration from Mexico.

When McCain’s family announced his decision to stop treatment, Meghan McCain thanked everyone for the love and generosity shown to the family over the last year, and wrote, “We could not have made it this far without you — you’ve given strength to carry on.”

Cindy McCain tweeted, "I love my husband with all of my heart. God bless everyone who has cared for my husband along this journey."

John McCain's marriage to his first wife, Carol, ended in divorce.

He is survived by another daughter, Sidney, and sons John Sidney McCain IV, or Jack, and Jimmy McCain. While he was married to Carol McCain, he adopted her two sons from an earlier marriage, Doug and Andy.