func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject], fetchCompletionHandler completionHandler: (UIBackgroundFetchResult) -> Void) {
// Use Alamofire to make an ajax call:
//...
let mutableURLRequest = NSMutableURLRequest(URL: URL)
let requestBodyData : NSMutableData = NSMutableData()
mutableURLRequest.HTTPBody = body
mutableURLRequest.HTTPMethod = "POST"
mutableURLRequest.setValue("Bearer " + accessToken, forHTTPHeaderField: "Authorization")
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
request(mutableURLRequest)
.responseJSON { (req, res, data, error) in
//We do not reach this code block !
// Save the incoming data to CoreData
completionHandler(UIBackgroundFetchResult.NewData)
}
}
dataTaskWithRequest
#import <AFNetworking.h>
NSString *urlString = @"https://api...";
NSURL *url = [NSURL URLWithString:urlString];
NSMutableURLRequest *mutableURLRequest = [NSMutableURLRequest requestWithURL:url];
NSMutableData *requestBodyData = [NSMutableData data];
mutableURLRequest.HTTPBody = requestBodyData;
mutableURLRequest.HTTPMethod = @"POST";
NSString *accessToken = @"accessToken";
NSString *value = [NSString stringWithFormat:@"Bearer %@", accessToken];
[mutableURLRequest setValue:value forHTTPHeaderField:@"Authorization"];
[mutableURLRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[[AFHTTPSessionManager manager] dataTaskWithRequest:mutableURLRequest completionHandler:^(NSURLResponse * _Nonnull response,
id _Nullable responseObject,
NSError * _Nullable error) {
//We do not reach this code block !
// Save the incoming data to CoreData
completionHandler(UIBackgroundFetchResultNewData);
}];