0

I'm struggling for quite some time to make this work. Code is in Objective-C and I do not know how to convert code below to use NSURLSession. Im getting error on return.

- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
{
    if ( _staticHandler != nil) {
        SBStaticHandlerResponse* mockResponse  = _staticHandler(request);

        if ( mockResponse != nil) {
            (*response) = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:nil headerFields:mockResponse.Headers];

            return mockResponse.Data;
        }
    }

    return [NSURLConnection sendSynchronousRequest:request returningResponse:response error:error];
    // ERROR: 'sendSynchronousRequest:returningResponse:error:' is unavailable: not available on watchOS
}

Based on link in comment, I updated code. Is this correct way?

- (NSData *)sendSynchronousRequest:(NSURLRequest *)request returningResponse:(NSURLResponse **)response error:(NSError **)error
{
    NSError __block *err = NULL;
    NSData __block *data;
    NSURLResponse __block *resp;

    [[[NSURLSession sharedSession] dataTaskWithRequest:request
                                    completionHandler:^(NSData* _data, 
    NSURLResponse* _response, NSError* _error) {
       resp = _response;
       err = _error;
       data = _data;
    }] resume];

    if ( _staticHandler != nil) {
        SBStaticHandlerResponse* mockResponse  = _staticHandler(request);
        if ( mockResponse != nil) {
            (*response) = [[NSHTTPURLResponse alloc] initWithURL:[request URL] statusCode:200 HTTPVersion:nil headerFields:mockResponse.Headers];

            return mockResponse.Data;
        }
    }
    return data;
    //return [NSURLConnection sendSynchronousRequest:request returningResponse:response error:error];
}
Win Fan
  • 77
  • 9
  • Possible dup of https://stackoverflow.com/questions/41997641/how-to-make-nsurlsession-post-request-in-swift – danh Jan 25 '20 at 00:10
  • Take a look at https://stackoverflow.com/a/37829399/2305521 – fpg1503 Jan 25 '20 at 01:29
  • 1
    (But you should probably avoid synchronous requests, especially in the Watch) – fpg1503 Jan 25 '20 at 01:30
  • @fpg1503 How I can switch it to NSURLSession Async.? – Win Fan Jan 25 '20 at 09:14
  • @danh this is solution in Swift. I need solution for Obj-C. – Win Fan Jan 25 '20 at 09:18
  • @fpg1503 - I updated question based on link in your comment. Is this ok? – Win Fan Jan 25 '20 at 09:33
  • 1
    `dataTaskWithRequest` works asynchronously. You cannot *return* anything. Don't force it to become synchronous. Write a completion handler. There are zillions of examples. – vadian Jan 25 '20 at 11:09
  • Can you make the question clearer? 1) remove the `NSURLConnection sendSynchronousRequest` code, that's not the code you want to make work. 2) remove the mock code, that's not part of a minimally verifiable test, 3) Show how you form the request passed to the NSURLSession code. 4) Show the error (very important to debug). – danh Jan 25 '20 at 16:25

0 Answers0