nsinvocation
nsinvocation

Reputation: 7637

Is 'performFetchWithCompletionHandler' called when no internet connection?

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

Upvotes: 1

Views: 326

Answers (2)

nsinvocation
nsinvocation

Reputation: 7637

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.

Upvotes: 1

Jeffery Thomas
Jeffery Thomas

Reputation: 42598

-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];
}

Upvotes: 0

Related Questions