How to receive async network message while the iph

2019-09-21 04:31发布

I have an iphone app receiving network message from server. It is ok when the app is running in frontend.

However, when the app is running in background, the app does not receive network message. After I bring it back to frondend, it starts receiving.

How to make the app can receive network message even it is running in background?

Thanks.

3条回答
看我几分像从前
2楼-- · 2019-09-21 05:11

First check this post: iphone - Connecting to server in background

Second, specifically speaking, when you app is in the background and is frozen by the OS, it won't have access to the internet, thus any call back from an async request won't be heard by your app.

Third, a possible strategy:

  • When you app is to switch to the background, immediately post a request to your server so your server would have an updated record about your app's latest status
  • When your app is to switch back to the foreground, again, let your server know about this by posting a request
  • It might happen, that your app is still in the background while your server just need to access it or send any message to it. In this scenario, try APNs (Apple Push Notification Service) provided by Apple. It's free and easy to implement. The possible outcome once you set up APNs for your app would be, the user gets an push message from your server and then decide to put your app back to the foreground. Though it's still totally up to the user, chances are good.
查看更多
别忘想泡老子
3楼-- · 2019-09-21 05:24

I'm not an iphone dev but for me, for all background apps, all network messages should be push to apple server and then push to the phone. There is no service like in Android.

查看更多
时光不老,我们不散
4楼-- · 2019-09-21 05:29

You've got two options for network connection while in the background:

  • Push Notifications. The app is not running, but your server can request to send a notification, which will then be sent to the iPhone, prompting the user to launch your app which can then update itself from your server thru a regular request.

  • Background network calls. Your app can request to keep a thread alive in the background in order to finish a network activity. You could use this to send a request when your app closes to check in with your server.

查看更多
登录 后发表回答