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

标签 objective-c json nsurlsession nsmutableurlrequest nsurlsessiondatatask

我有一个简单的 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 请求,我不明白为什么数据不会加载。

最佳答案

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

Allow Arbitrary Loads

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

App Sandbox Setting

关于objective-c - 网络请求失败 - NSURLSession - App Sandbox - Xcode 9,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46971824/

相关文章:

ios - 放入 ScrollView 时出现表格 View 框架问题

json - 在 swift 4 中解析 JSON 时出错

python - 将Python中的JSON数据解析为CSV文件

swift - NSURLSession 和 Twitter 流 API

ios - 使用 NSURLSessionDownloadTask 时使用 resumeData 的正确方法是什么?

ios - 在 Popover 中显示 ImagePicker - ipad

iphone - 根据所选国家/地区获取州列表

android - JSON异常 : No value for id after completing paypal payment

swift - NSURLSession 初始化错误

iphone - 从 nsdate 获取一周中的任何一天?