如何实现UIRequiresPersistentWifi在后台工作应用程序

时间:2015-05-28 18:28:42

标签: ios iphone wifi background-process

我的应用程序在后台需要持久的wifi连接,我认为唯一的方法是将UIRequiresPersistentWifi应用于info.plist文件。所以,下面是我添加到info.plist的内容,

我在UIRequiresPersistentWifi添加了YES

我在Required device capabilities

添加了wifi

我在代码中创建了NSInputStreamNSOutputStream个流。即使在完成所有这些套接字后也无法在后台运行。我还缺少什么?

我应该向- (void)applicationDidEnterBackground:(UIApplication *)application委托方法添加任何代码吗?

1 个答案:

答案 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;
    });
}