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.

Thanks for your time, Dan.

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.

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.

objective c - Network requests are failing - App Sandbox, Network requests are failing - NSURLSession - App Sandbox - Xcode 9 · Ask Question. Asked 2 years, 3 months ago. Active 1 year, 4 months ago. Viewed 2k  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. 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.

You have to turn off the "App Sandbox".

Go to:

xcode Project-> Capabilities -> App SandBox

Network requests are failing - NSURLSession, 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  It's network friendly and requires less system resources on both the client and the server. HTTP/2 is fully multiplexed. That means that a new request does not need to wait until the server sends the response for the previous request. HTTP/2 has request priorities, so that more important resources can be delivered at a higher priority to the client.

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];
}

NSURLSessionDownloadTask, iOS 7.0+; macOS 10.9+; Mac Catalyst 13.0+; tvOS 9.0+; watchOS 2.0+ You can also resume downloads that failed because of network connectivity problems. During download, the session periodically calls the delegate's URLSession: or move it to a permanent location in your app's sandbox container directory. The following tasks describe common operations with the NSURLSession class, the NSURLConnection class, and related classes. Retrieving the Contents of a URL without Delegates. If you just need to retrieve the contents of a URL and do something with the results at the end, in OS X v10.9 and later or iOS 7 and later, you should use the NSURLSession class.

Networking In Swift With URLSession – LearnAppMaking, And what Swift code can you use to make HTTP networking requests? and learn how to build great iOS 13 apps with Swift 5 and Xcode 11. A URL session task that uploads data to the network in a request body. NSURLSession Data Delegate A protocol that defines methods that URL session instances call on their delegates to handle task-level events specific to data and upload tasks.

URLSession Tutorial: Getting Started, In this URLSession tutorial, you'll learn how to create HTTP requests as well as Laso-Marsetti updated this tutorial for Swift 5.0, Xcode 10.2 and iOS 12. Your job is to move it to a permanent location in your app's sandbox  An NSURLSession Download Task is a concrete subclass of NSURLSession Task.Most of the methods associated with this class are documented in NSURLSession Task.. Download tasks directly write the server’s response data to a temporary file, providing your app with progress updates as data arrives from the server.

Network Stubbing options for XCTest and XCUITest in Swift, The network testing of the iOS app written in Swift can be performed at Unit, libraries are DVR which make fake NSURLSession requests for iOS apps. Apple has XCUITest framework to cover Xcode UI testing. result of the test depends on the network, if the network went off, out test will fail. Aug 9, 2019 · 5 min read  Empties all cookies, caches and credential stores, removes disk files, flushes in-progress downloads to disk, and ensures that future requests occur on a new socket. session Description An app-defined descriptive label for the session.

Comments
  • 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.
  • 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.
  • Also my app is for the macOS platform, so [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES]; is not applicable.