Answer the question
In order to leave comments, you need to log in
How to translate this code from swift to objective-c?
help translate swift code to objective-c
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)
}
}
Answer the question
In order to leave comments, you need to log in
Something like this.
I'm not sure what called that method dataTaskWithRequest
. But it should work in this case too.
#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);
}];
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question