当应用程序暂时进入后台时,我正在尝试维护一个MultipeerConnectivity“会话”,所以我考虑使用后台任务,我在这里见过几次.问题是我不知道如何用UIBackgroundTask“维护”会话,有人能给我一个提示吗?
我不关心广告商/浏览器,它可以阻止他们,但我希望会话不要中断,因为现在重新连接是超级错误。
发布于 2014-09-16 01:37:57
根据苹果的文档“如果应用程序进入后台,框架将停止广告和浏览,并断开任何打开的会话。当返回到前台时,框架将自动恢复广告和浏览,但开发人员必须重新建立任何封闭会话”(参见:苹果文档 )。
扩展连接的一种方法如下
回答我自己的问题,希望它能帮助处于同样情况的人。对于iOS开发新手来说,“使用后台服务”简单意味着打开目标的“功能”选项卡中的“后台模式”选项。仅这一项就会给你的应用程序10分钟左右的生命,然后它才会被杀死。
但是,当应用程序转到后台时,我使用"backgroundTimeRemaining“来知道我还剩下多少时间,它只是从180秒开始(以秒计,3分钟),然而,打印循环仍然工作了三分钟,这意味着需要手动编写当时间到达时应该发生的事情。
对于多点连接,这足以在应用程序进入后台时保持连接活动,并且它仍将接收所有消息/流,而不存在任何问题。
为了稳定起见,我做了以下的清理工作:
的代表。h
@property (nonatomic) UIBackgroundTaskIdentifier backgroundTask; //declaring a background task在应用程序中。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
self.backgroundTask = [application beginBackgroundTaskWithExpirationHandler:^
{
//This is called 3 seconds before the time expires
//Here: Kill the session, advertisers, nil its delegates,
// which should correctly send a disconnect signal to other peers
// it's important if we want to be able to reconnect later,
// as the MC framework is still buggy
[application endBackgroundTask:self.backgroundTask];
self.backgroundTask = UIBackgroundTaskInvalid; //Invalidate the background task
}];
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Here: We should init back the session, start the advertising and set the delegates from scratch
// This should allow the app to reconnect to the same session with more often than not
self.backgroundTask = UIBackgroundTaskInvalid; //Here we invalidate the background task if the timer didn't end already
}发布于 2014-09-12 06:05:02
我曾在苹果开发者论坛上问过同样的问题。苹果的一位员工告诉我,当你的应用程序不在前台时,基本上所有的多点连接都应该被认为是禁止的。
https://stackoverflow.com/questions/25801665
复制相似问题