Created
December 27, 2016 21:55
-
-
Save akatreyt/242ba195f31d1b31189b3043dfcc1397 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { | |
UIApplicationState state = [application applicationState]; | |
if (state == UIApplicationStateBackground || (state == UIApplicationStateInactive)) { | |
NSDictionary *aps = userInfo[@"aps"]; | |
if (aps) { | |
NSLog(@"Background fetch started..."); | |
NSString *urlString = [NSString stringWithFormat:@"https://www.apple.com/ac/structured-data/images/knowledge_graph_logo.png?201612081039"]; | |
NSURLSession *session = [NSURLSession sharedSession]; | |
[[session dataTaskWithURL:[NSURL URLWithString:urlString] | |
completionHandler:^(NSData *data, | |
NSURLResponse *response, | |
NSError *error) { | |
NSHTTPURLResponse *httpResp = (NSHTTPURLResponse*) response; | |
if (!error && httpResp.statusCode == 200) { | |
completionHandler(UIBackgroundFetchResultNewData); | |
NSLog(@"Background fetch completed..."); | |
} else { | |
NSLog(@"%@", error.description); | |
completionHandler(UIBackgroundFetchResultFailed); | |
NSLog(@"Background fetch Failed..."); | |
} | |
} | |
] resume | |
]; | |
} | |
} else if (state == UIApplicationStateInactive) { | |
// user tapped notification | |
completionHandler(UIBackgroundFetchResultNoData); | |
} else { | |
// app is active | |
completionHandler(UIBackgroundFetchResultNoData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment