San Francisco - Apple Inc sold more iPhones than Wall Street expected in the third quarter and forecast revenue in the current period would top many analysts’ targets, soothing fears that demand for Apple’s most important product had hit a wall.

Its shares rose more than 7 percent in after-hours trading.

The world’s most valuable publicly traded company said it sold 40.4 million iPhones in the third quarter, down 15 percent from the year-ago quarter but slightly more than the average analyst forecast of 40.02 million, according to research firm FactSet StreetAccount.

IPhone sales dropped for the second straight quarter, pushing down Apple’s total revenue 14.6 percent in the fiscal third quarter, ended June 25.

Demand for Apple’s phones has waned in China, partly because of economic uncertainty there, and has also slowed in more mature markets as people tend to hold on to their phones for longer.

“China was a major letdown and I’m anxious to get more details as to what drove the declines,” said Patrick Moorhead, an analyst at Moor Insights & Strategy. “Samsung and Huawei are much more competitive now than a year ago and the Chinese economy is not doing well at all.”

Moorhead said, however, that increased services revenue - which includes the App Store and iCloud - was a “very big bright spot for Apple.”

Chief Financial Officer Luca Maestri said Apple’s performance had topped his expectations in a quarter weighed down by tough foreign exchange rates and difficult comparisons with blockbuster iPhone 6 sales from the previous year.

Apple reduced channel inventory by $3.6 billion, exceeding the $2 billion expected reduction, meaning sales were better than they appeared, Maestri said.

Customer demand “was better than what is implied in our results and better than we had anticipated,” he told Reuters in an interview.

Sales of the iPhone fell last quarter for the first time since the gadget’s release in 2007, dropping 16.3 percent. Maestri projected the gadget’s average selling price to rise in the September quarter.

Sales of iPhones account for about two-thirds of Apple’s total sales. The iPhone lineup includes the iPhone 6S and 6S Plus, as well as the smaller and cheaper iPhone SE.

Apple’s quarterly net profit fell 27 percent to $7.8 billion, while revenue of $42.36 billion beat analysts’ average estimate of $42.09 billion, according to Thomson Reuters I/B/E/S.

Sales in Greater China, once touted as Apple’s next growth engine, decreased 33.1 percent, compared with a 112.4 percent growth in the year earlier quarter and a near 26 percent fall in the second quarter.

Investors are sensitive to any signs of trouble in China, one of the company’s largest markets by revenue.

Maestri attributed the drop to channel inventory reduction in the nation, foreign exchange headwinds and a general downturn in the Chinese economy.

“It is very clear that there are some signs of economic slowdown in China, and we will have to work through them,” he said. “We understand China well and we remain very, very optimistic about the future there.”

Apple’s services business, which includes the App Store, Apple Pay, iCloud and other services generated nearly $6 billion in revenue, up 18.9 percent from the previous year.

As iPhone sales level off, Apple is attempting to wring more revenue out of its existing base of users by emphasizing services such as the App Store, Apple Music, storage center iCloud and mobile wallet Apple Pay. Such services emerged as Apple’s second-largest business after the iPhone for the first time in the company’s second quarter, eclipsing gadgets such as the iPad and the Mac.

That shift in the business bodes well for Apple because gross margins on services are better than the average for the rest of the company, Maestri said.

“It’s a great business because it is recurring in nature and more linked to our installed base,” he said.

The company forecast fourth-quarter revenue of $45.5 billion to $47.5 billion, largely above Wall Street’s average estimate of $45.71 billion, according to Thomson Reuters I/B/E/S.

The forecast, covering the quarter ending September, will likely include at least the first weekend of sales of the iPhone 7 range, which Apple is expected to launch in September.

Up to Tuesday’s close, Apple’s shares had fallen about 8.2 percent since the start of the year. Shares rose 7.1 percent to $103.57 in after-hours trade following publication of results.