Apple delivered stronger-than-expected results Tuesday for the past quarter as gains in services helped offset slumping iPhone sales, sparking a rally in shares of the technology giant. California-based Apple reported a 16% drop in profits in the just-ended quarter to $11.6 billion, as overall revenues dipped 5% to $58 billion.

Apple shares rallied 5% in after-hours trade, with the results showing better-than-anticipated results from its new products and services in the fiscal second quarter to March 30. “Our March quarter results show the continued strength of our installed base of over 1.4 billion active devices, as we set an all-time record for services, and the strong momentum of our wearables, home and accessories category, which set a new March quarter record,” said chief executive Tim Cook.

“We delivered our strongest iPad growth in six years and we are as excited as ever about our pipeline of innovative hardware, software and services.”

Apple has been scrambling to develop new revenue streams amid stagnant growth and increased competition in the smartphone market, which has been the company’s main profit driver.

The latest quarterly results showed revenue from iPhone sales slumped 17% to $31 billion. Apple has stopped reporting iPhone unit sales.

China-based Huawei outsold Apple’s iPhones in the first quarter of this year, seizing the California company’s second place spot in a tightening smartphone market dominated by Samsung, according to Chinese-owned market tracker IDC.

A total of 310.8 million smartphones were shipped globally during the first three months of this year in a 6.6% decline from the same period in 2018, preliminary IDC data showed. It was the sixth consecutive quarter of decline for global smartphone shipments.

Apple had a challenging first quarter, with iPhone shipments dropping by a “staggering” 30.2% from a year earlier to 36.4 million units, IDC reported.

Huawei, meanwhile, weighed in with 50.3% growth to ship 59.1 million smartphones and put it within “striking distance” of Samsung, according to IDC.

South Korean consumer electronics behemoth Samsung saw smartphone shipments drop 8.1 percent to 71.9 million in the first quarter.

But the money Apple took in from services or digital content grew 16% to $11 billion, reflecting growth in music, Apple Pay and other new offerings from the firm.

Apple also saw growth in its wearables which include its Apple Watch, earbuds and its smart HomePod speaker.

The company also said it would spend an additional $75 billion for share repurchases and boost its dividend, delivering more cash to its investors.

It ended the quarter with some $225 billion in cash.