objective-cjsonnsurlsessionnsmutableurlrequestnsurlsessiondatatask

Network requests are failing - NSURLSession - App Sandbox - Xcode 9


I have a simple GET request that I am trying to run in my macOS application. However I keep getting the following error:

A server with the specified hostname could not be found.

The URL I am trying to download JSON data from is:

https://suggestqueries.google.com/complete/search?client=safari&q=mercedes

If I test it in my browser or in an online API tester website (such as Hurl.it), the request works fine. A JSON file is then downloaded automatically.

However running the request via my macOS app does not work. Here is my code:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://suggestqueries.google.com/complete/search?client=safari&q=mercedes"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
    
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    
[[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        
    NSLog(@"Data: %@", data);
    NSLog(@"Response: %@", response);
    NSLog(@"Error: %@", error);
        
}] resume];

Here is the full error log:

dnssd_clientstub ConnectToServer: connect()-> No of tries: 1 dnssd_clientstub ConnectToServer: connect()-> No of tries: 2 dnssd_clientstub ConnectToServer: connect()-> No of tries: 3 dnssd_clientstub ConnectToServer: connect() failed

path:/var/run/mDNSResponder Socket:6 Err:-1 Errno:1 Operation not permitted 2017-10-27 09:58:31.610493+0100 search suggestions [] nw_resolver_create_dns_service_locked DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563) TIC TCP Conn Failed [1:0x600000164080]: 10:-72000 Err(-65563) Task <12212C3B-8606-49C2-BD72-AEBD575DB638>.<1> HTTP load failed (error code: -1003 [10:-72000]) Task <12212C3B-8606-49C2-BD72-AEBD575DB638>.<1> finished with error - code: -1003

Data: (null) Response: (null) Error: Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x60400004fa20 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey=https://www.suggestqueries.google.com/complete/search?client=safari&q=mercedes, NSErrorFailingURLKey=https://www.suggestqueries.google.com/complete/search?client=safari&q=mercedes, _kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not be found.}

What am I doing wrong? It's just a simple GET request, I don't understand why the data won't load.


Solution

  • I figured out what was wrong, even though I had set Allow Arbitrary Loads to YES, this is no longer enough to enable network requests.

    Allow Arbitrary Loads

    There is a new setting in Xcode 9 called App Sandbox that can stop incoming/outgoing network connections too! I had to turn this setting off and then all network requests started to work.

    App Sandbox Setting