网络请求失败 - NSURLSession - App Sandbox - Xcode 9

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

我有一个简单的 GET 请求,我正在尝试 运行 在我的 macOS 应用程序中。但是我不断收到以下错误:

A server with the specified hostname could not be found.

我正在尝试从中下载 JSON 数据的 URL 是:

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

如果我在我的浏览器或在线 API 测试网站(例如 Hurl.it)中测试它,请求工作正常。然后会自动下载一个 JSON 文件。

但是 运行通过我的 macOS 应用程序发送请求不起作用。这是我的代码:

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

这是完整的错误日志:

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

我做错了什么?只是一个简单的GET请求,我不明白为什么数据加载不出来。

请尝试以下代码

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

我找出问题所在,即使我已将 Allow Arbitrary Loads 设置为 YES,这已不足以启用网络请求。

Xcode 9 中有一个名为 App Sandbox 的新设置,它也可以停止 incoming/outgoing 网络连接!我必须关闭此设置,然后所有网络请求才开始工作。

您必须关闭 "App Sandbox"。

转到:

xcode Project-> Capabilities -> App SandBox

在 XCode 11.5 中,我必须检查这两个标志