在iOS中处理后台任务的正确方法是什么 [英] What is the proper way to handle background tasks in iOS

查看:159
本文介绍了在iOS中处理后台任务的正确方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个voip应用程序,它需要在后台运行。根据我的理解,这些是我需要做的事情:

I have a voip app and it needs to run in the background. To my understanding these are the things I need to do:


  1. 将应用程序标记为voip。

  2. 将应用程序不在后台运行标志设置为NO。

  3. 设置一个到期处理程序,这段代码可以延长标准的10分钟执行时间。

  4. 更多?

  1. Flag the app as voip.
  2. Set the 'application does not run in background' flag to NO.
  3. Set an expiration handler, a piece of code that extends the standard 10 minutes of execution time you get.
  4. More?

我在info.plist文件中设置了两个标志,然后我得到了10分钟。我尝试了这篇文章中的建议。这是我的代码:

I set both flags in the info.plist file and I get my 10 minutes. I tried what is suggested in this post. Here is my code:

//in didFinishLaunchingWithOptions:
expirationHandler = ^{
    NSLog(@"ending background task");
    [[UIApplication sharedApplication] endBackgroundTask:bgTask];

    NSLog(@"restarting background task");
    bgTask = UIBackgroundTaskInvalid;
    bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler];

    NSLog(@"finished running background task");
};

//in applicationDidEnterBackground
NSLog(@"entering background mode");
bgTask = UIBackgroundTaskInvalid;
bgTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:expirationHandler];

// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // inform others to stop tasks, if you like
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MyApplicationEntersBackground" object:self];

    //this while loop is just here for testing
    inBackground = true;
    while (inBackground) {
        NSLog(@"stayin alive!!"); //this keeps going forever
        sleep(10);
    }
});



情况:



我用的是处理与我们的Web服务通信的第三方库。该服务是CommuniGate专业版服务器。我通过库接收来自联系人的在线状态更新(在线/离线)和即时消息。该库是CommuniGate的ximss库,它们制作的协议类似于xmpp,用于基于xml的sip请求,以及IM和状态。当用户登录应用程序时,他会看到他的联系人(CmmuniGate好友列表),他可以选择拨打一个。在发送ximss验证消息并且另一方接受了呼叫之后,它记录呼叫的开始时间并开始临时呼叫。

The situation:

I use a third party library that handles the communication with our webservice. The service is a CommuniGate pro server. I receive presence updates (online/offline) and instant messages from contacts via the library. The library is CommuniGate's ximss library, a protocol they made which is similar to xmpp and is used for xml-based sip requests, as well as IM and presence. When the user logs in to the app, he sees his contacts (CmmuniGate friends list) and he can choose to call one. After a ximss verification message has been sent and the other side accepted the call it logs the start time of the call and starts a facetime call.

当应用程序通过按主页按钮进入后台时,我开始看到'在日志中保留活着消息,每隔十分钟就会看到它重新启动后台任务。

当应用程序按下电源按钮进入后台时,保持活跃消息开始显示为十几分钟后,它重新启动后台任务,并开始大约每50-100毫秒重新启动它。

我现在一直很好,甚至它吃电池,因为我有时间工作更新和我们的用户不拥有ipads,我们这样做。我现在的问题是ximss库丢失了它的连接(它是基于会话的)。我可以在库中重新启动会话,但这意味着需要进行相当多的数据传输来获取联系人列表,而一些用户使用3g。

我无法编辑库的源代码,也无法看到它,所以我不知道它是否以正确的方式创建了套接字。

When the app enters the background by pressing the home button, I start seeing the 'stayin alive' message in the log and every ten minutes I see that it restarts the background task.
When the app enters the background by pressing the power button, the 'staying alive' messages start showing up for ten minutes, after that it restarts the background task and start restarting it about every 50-100 miliseconds.
I would've been fine with this for now, even it eats battery, because I have time to work on updates and our users don't own the ipads, we do. The problem for me now is that the ximss library loses it's connection (it is session-based). I could restart the session in the library, but this means quite a bit of data transfer to fetch the contacts list and some users use 3g.
I can't edit the library's source, nor can I see it, so I don't know if it creates the sockets the right way.

我需要做些什么来正确处理这两种情况?我甚至不明白为什么会有区别。

What do I have to do to handle both situations correctly? I don't even understand why there is a difference.

推荐答案

你不能重新扩展像这样的后台任务;您的应用可能会被终止。如果这样做,那是因为你启用了后台 voip 模式,而不是因为你正在重新启动后台任务。

You cannot re-extend background tasks like this; your app is likely to be terminated. If this is working, it's because you have the background voip mode enabled, not because you are restarting the background task.

设置 voip plist条目后,iOS将尽可能长时间保持您的应用程序处于活动状态,并在终止时重新启动它。来自实施VoIP应用程序

Once you have set the voip plist entry, iOS will attempt to keep your app alive as long as possible and restart it if it does get terminated. From Implementing a VoIP App:


在UIBackgroundModes键中包含voip值让系统
知道它应该允许应用程序在后台运行,需要
来管理其网络套接字。具有此密钥的应用程序也会在系统启动后立即在后台重新启动
,以确保
VoIP服务始终可用。

Including the voip value in the UIBackgroundModes key lets the system know that it should allow the app to run in the background as needed to manage its network sockets. An app with this key is also relaunched in the background immediately after system boot to ensure that the VoIP services are always available.

除了设置此密钥外,如果您需要定期运行代码以保持您的voip连接处于活动状态,您可以使用 setKeepAliveTimeout:handler: <$ c $上的方法c> UIApplication 。

In addition to setting this key, if you need to periodically run code to keep your voip connection alive, you can use the setKeepAliveTimeout:handler: method on UIApplication.

另请参阅开发VoIP应用程序的提示


实施VoIP应用有几个要求:

There are several requirements for implementing a VoIP app:


  1. 广告d应用程序的Info.plist文件的UIBackgroundModes键。将此键的值设置为包含voip字符串的数组。

  1. Add the UIBackgroundModes key to your app’s Info.plist file. Set the value of this key to an array that includes the voip string.

配置应用程序的一个套接字用于VoIP使用。

Configure one of the app’s sockets for VoIP usage.

在移至后台之前,请调用setKeepAliveTimeout:handler:方法以定期执行
处理程序。您的应用可以使用此处理程序维护其
服务连接。

Before moving to the background, call the setKeepAliveTimeout:handler: method to install a handler to be executed periodically. Your app can use this handler to maintain its service connection.

配置音频会话以处理与活动使用之间的转换。

Configure your audio session to handle transitions to and from active use.

为确保在iPhone上获得更好的用户体验,请使用核心电话框架调整与基于小区的
电话呼叫相关的行为;请参阅核心电话框架参考。

To ensure a better user experience on iPhone, use the Core Telephony framework to adjust your behavior in relation to cell-based phone calls; see Core Telephony Framework Reference.

要确保VoIP应用程序的良好性能,请使用系统配置框架检测网络更改并允许您的应用程序
尽可能多地睡觉。

To ensure good performance for your VoIP app, use the System Configuration framework to detect network changes and allow your app to sleep as much as possible.


您需要的几乎所有文档都是在Apple开发者网站上。

Almost all of the documentation you need is on the Apple developer site.

这篇关于在iOS中处理后台任务的正确方法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆