我的应用程序在后台需要持久的wifi连接,我认为唯一的方法是将UIRequiresPersistentWifi
应用于info.plist文件。所以,下面是我添加到info.plist的内容,
我在UIRequiresPersistentWifi
添加了YES
。
我在Required device capabilities
wifi
我在代码中创建了NSInputStream
,NSOutputStream
个流。即使在完成所有这些套接字后也无法在后台运行。我还缺少什么?
我应该向- (void)applicationDidEnterBackground:(UIApplication *)application
委托方法添加任何代码吗?
答案 0 :(得分:0)
是的,我们必须在applicationDidEnterBackground
中添加代码,如下所示。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
bgTask = [application beginBackgroundTaskWithExpirationHandler:^{
// Clean up any unfinished task business by marking where you
// stopped or ending the task outright.
[application endBackgroundTask:bgTask];
bgTask = UIBackgroundTaskInvalid;
}];
// Start the long-running task and return immediately.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// Do the work associated with the task, preferably in chunks.
bgTask = UIBackgroundTaskInvalid;
});
}