ios 错误域=NSURLErrorDomain 代码=-1003“无法找到具有指定主机名的服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27258702/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 03:52:21  来源:igfitidea点击:

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found

iosobjective-cnsurlconnection

提问by shriniwas_ayyer

I get this error when I try running the app on a device. I do not get this error when I run it on the simulator and the post request works fine. This is code snippet I have:

当我尝试在设备上运行应用程序时出现此错误。当我在模拟器上运行它时,我没有收到此错误,并且发布请求工作正常。这是我拥有的代码片段:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url    cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:300.f];
[request setHTTPMethod:@"POST"];

NSData* data = [paramString dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:data];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

Any help is appreciated. Thanks

任何帮助表示赞赏。谢谢

回答by JoGoFo

This error would typically suggest a DNS related issue. Check the DNS settings on your device and confirm you can browse the internet using Safari or another browser (double check by visiting a site you have not visited before to ensure it is not cached).

此错误通常表示与 DNS 相关的问题。检查您设备上的 DNS 设置,并确认您可以使用 Safari 或其他浏览器浏览互联网(通过访问您之前未访问过的站点来仔细检查以确保它没有被缓存)。

If there is a url you can GET (instead of POST) on the same server, try visiting it directly in Safari on the device.

如果在同一台服务器上有一个可以 GET(而不是 POST)的 url,请尝试直接在设备上的 Safari 中访问它。

回答by Baskar

The simulator uses your computers network connection, so I recommend checking the System Preferences > Network > Advanced > Proxies > Automatic Proxy Configuration : You must disable Automatic conf.

模拟器使用您的计算机网络连接,因此我建议检查系统偏好设置 > 网络 > 高级 > 代理 > 自动代理配置:您必须禁用自动配置。

回答by sarfata

With Xcode 11.2.1, working on a macOS project, I had the same issue.

使用 Xcode 11.2.1 处理 macOS 项目时,我遇到了同样的问题。

The fix here was:

这里的修复是:

  • Click on project in project explorer
  • Select the Signing & Capabilities tab
  • Check "Network: Outgoing connections (client)" checkbox in the AppSandbox section
  • 在项目资源管理器中单击项目
  • 选择签名和功能选项卡
  • 选中 AppSandbox 部分中的“网络:传出连接(客户端)”复选框