Network requests are failing - NSURLSession - App Sandbox - Xcode 9
Asked Answered
B

4

5

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.

Bearden answered 27/10, 2017 at 9:29 Comment(0)
B
16

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

Bearden answered 27/10, 2017 at 10:41 Comment(1)
I just spent two days trying to debug why I couldn't get my WebView's to work. It was the sandbox setting. Thanks for this post.Tic
M
7

In XCode 11.5 I had to check these two flags

enter image description here

Mcglone answered 31/7, 2020 at 17:42 Comment(1)
Saved my day, that's exactly right.Squeegee
M
3

enter image description here

You have to turn off the "App Sandbox".

Go to:

xcode Project-> Capabilities -> App SandBox

Malley answered 5/9, 2018 at 13:29 Comment(0)
S
0

Please try below code

@property (nonatomic, strong) NSURLConnection *connection;

NSMutableString *urlString = [NSMutableString stringWithString:BASE_URL];

[urlString appendFormat:@"%@",apiName]; //apiName —> Webservice Name

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];

[request setValue:@"gF!DeEfFrjqaAaD$gH#Mn@w(z" forHTTPHeaderField:@"PC-API-KEY"];  //Optional Parameter pass if required key
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[request setHTTPMethod:@"POST"]; //if your required Get than  [request setHTTPMethod:@"GET"];

[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:[parameterDict toJSON]];
[request setTimeoutInterval:45.0];

self.connection =[[NSURLConnection alloc] initWithRequest:request delegate:self];

if (self.connection) {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
    self.downLoadedData = [NSMutableData data];
}
Sholom answered 27/10, 2017 at 9:43 Comment(2)
I don't see how this will solve my problem. It's basically the same code as mine, except it uses the delegate methods to load in the data, rather than a block.Bearden
Also my app is for the macOS platform, so [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; is not applicable.Bearden

© 2022 - 2024 — McMap. All rights reserved.