A
A
Andrew2017-05-29 14:55:27
Objective-C
Andrew, 2017-05-29 14:55:27

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)
    }
}

https://stackoverflow.com/questions/32031320/async... code from this issue

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
oltv00, 2017-06-09
@undefined_title

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 question

Ask a Question

731 491 924 answers to any question