iPhone应用程序在后台运行时如何接收异步网络消息

发布于 2024-10-10 15:05:38 字数 146 浏览 0 评论 0原文

我有一个 iPhone 应用程序从服务器接收网络消息。当应用程序在前端运行时就可以了。

但是,当应用程序在后台运行时,应用程序不会接收网络消息。当我把它带回前端后,它开始接收。

如何让应用程序在后台运行时也能接收网络消息?

谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾城月光淡如水﹏ 2024-10-17 15:05:38

在后台时,您有两个网络连接选项:

  • 推送通知。该应用程序未运行,但您的服务器可以请求发送通知,然后该通知将发送到 iPhone,提示用户启动您的应用程序,然后该应用程序可以通过常规请求从您的服务器进行自我更新。

  • 后台网络调用。您的应用程序可以请求在后台保持线程处于活动状态,以便完成网络活动。您可以使用它在您的应用程序关闭时发送请求以检查您的服务器。

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.

梅窗月明清似水 2024-10-17 15:05:38

首先查看这篇文章:iphone - 在后台连接到服务器

其次,具体来说,当您的应用程序位于后台并被操作系统冻结时,它将无法访问互联网,因此您的应用程序不会听到来自异步请求的任何回调。

第三,一个可能的策略:

  • 当你的应用程序要切换到后台时,立即向你的服务器发出请求,这样你的服务器就会有关于你的应用程序最新状态的更新记录
  • 当你的应用程序要切换回前台时,再次让您的服务器通过发布请求来了解这一点。
  • 可能会发生这样的情况:您的应用程序仍然在后台,而您的服务器只需要访问它或向它发送任何消息。在这种情况下,请尝试 Apple 提供的 APNs(Apple 推送通知服务)。它是免费且易于实施的。一旦您为应用程序设置 APN,可能的结果是,用户从您的服务器收到推送消息,然后决定将您的应用程序放回前台。尽管这仍然完全取决于用户,但机会还是很大的。

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.
虐人心 2024-10-17 15:05:38

我不是 iPhone 开发人员,但对我来说,对于所有后台应用程序,所有网络消息都应该推送到苹果服务器,然后推送到手机。没有像Android那样的服务。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文