iPhone 应用程序可以在后台因重大位置变化而唤醒进行网络活动吗?

发布于 2024-10-25 12:17:33 字数 309 浏览 2 评论 0原文

我正在开发一个应用程序,可以监视后台的重大位置变化。我一直在阅读有关 ios4 和应用程序生命周期的所有答案(好吧,我想全部!),但我不知道当应用程序在后台唤醒时我是否可以进行任何网络访问由于位置发生重大变化。

该应用程序当前通过 TCP 套接字进行网络访问。当应用程序暂停时,套接字将关闭。

我可以重新连接套接字,接收一些数据,发送新位置,然后由于接收位置更改事件而仍在后台关闭套接字吗?在办公室里静止不动时很难进行测试!我们可以假设网络活动只需不到 10 秒即可完成。还假设该应用程序未注册为 voip 应用程序(可以/应该吗?...)

有什么想法吗?

I'm working on an app that monitors significant location changes in the background. I've been reading all the answers (well, I think all!) about ios4 and the application lifecycle, but what I can't figure out is whether or not I can do any network access when the app is woken up in the background as a result of a significant location change.

The app currently does network access via TCP sockets. The socket is shutdown when the app is suspended.

Can I re-connect the socket, receive some data, send the new location and then shutdown the socket all while still in the background as a result of receiving the location change event? It's hard to test while stationary in the office! We can assume that the network activity takes less than 10 seconds to complete. Also assume the app isn't registered as a voip app (could/should it be? ...)

Any ideas?

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

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

发布评论

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

评论(2

骑趴 2024-11-01 12:17:33

是的。您可以根据重大更改事件进行网络处理。

当您因重大变化(从终止或挂起状态)而被唤醒进入后台状态时,您可以发出 http 请求。如果您发现您的应用程序在完成 HTTP 请求之前被终止,您可以通过 beginBackgroundTaskWithExpirationHandler:UIApplication 上的方法。

我花了相当多的时间在短途火车旅行上来测试这一点。 :)

Yep. You can do your network processing based on significant change events.

When you get woken up into the background state on significant change (from terminated or suspended state), you can make your http request. If you find your app is being terminated before you finish your HTTP request, you can ask for additional background processing time via the beginBackgroundTaskWithExpirationHandler: method on UIApplication.

I spent a fair bit of time on short train trips to test this out. :)

避讳 2024-11-01 12:17:33

我只是想通过一些细节确认 RedBlueThing 的答案。

我现在使用以下方法来完成此任务。 bgTask 被声明为 UIBackgroundTaskIdentifier

bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:
           ^{
                 [[UIApplication sharedApplication] endBackgroundTask:bgTask];
            }];

// Now call methods doing network activity
// ...
// WHEN I KNOW THE LAST ACTION HAS COMPLETED USE THIS BLOCK OF CODE

if (bgTask != UIBackgroundTaskInvalid)
{
      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
}

希望这会有所帮助,因为我看到很多人在看这个问题。

I'd just like to confirm RedBlueThing's answer with some detail.

I now use the following to accomplish this. bgTask is declared as UIBackgroundTaskIdentifier

bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:
           ^{
                 [[UIApplication sharedApplication] endBackgroundTask:bgTask];
            }];

// Now call methods doing network activity
// ...
// WHEN I KNOW THE LAST ACTION HAS COMPLETED USE THIS BLOCK OF CODE

if (bgTask != UIBackgroundTaskInvalid)
{
      [[UIApplication sharedApplication] endBackgroundTask:bgTask];
      bgTask = UIBackgroundTaskInvalid;
}

Hope this helps out since I see quite a few people looking at the question.

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