Is 'performFetchWithCompletionHandler' called when no internet connection?

…衆ロ難τιáo~ 提交于 2019-12-11 09:45:44

问题


Will the UIApplicationDelegate call performFetchWithCompletionHandler if device is not connected to the internet ? The documentation isn't clear in this case.


回答1:


After some tests I can claim that performFetchWithCompletionHandler delegate method is not called if device is not connected to the internet. Tested on iOS8 and iOS9.




回答2:


-application:performFetchWithCompletionHandler: isn't called when a download has completed. It's called by the system to give your app a chance to download data. You do normal error handling as you see fit.

-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
    NSURL *URL = // Your URL
    [[[NSURLSession sharedSession] dataTaskWithURL:URL completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        if (error != nil) {
            // Handle Error
            completionHandler(UIBackgroundFetchResultFailed);
            return;
        }

        // Process the data
        completionHandler(UIBackgroundFetchResultNewData);
    }] resume];
}


来源:https://stackoverflow.com/questions/34176980/is-performfetchwithcompletionhandler-called-when-no-internet-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!