NSURLSessionDataTask in App Delegate

508 views Asked by At

I am trying to run a NSURLSessionDataTask in my app delegate on a background fetch but although I get valid data from the completion handler the app isn't firing the method. Here is my code

 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {

    NSURLSession *serverConfigure = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSessionConfiguration *serverSession = [NSURLSession sessionWithConfiguration:serverConfigure delegate:self delegateQueue:nil];

    NSURLSessionDataTask *serverDownload = [serverSession dataTaskWithURL:[NSURL URLWithString:@"myurl"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSArray *downloadedData = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

        [self handleData:downloadedData];

    }];

    [serverDownload resume];

}
1

There are 1 answers

2
Bevan On

In backgroundFetch your application will call this method in background -(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{

for this you have to activate background fetch mode ...just go through this link http://www.appcoda.com/ios7-background-fetch-programming/