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

情到浓时终转凉″ 提交于 2019-12-10 12:54:22

问题


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


回答1:


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

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




回答2:


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.




回答3:


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

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


来源:https://stackoverflow.com/questions/27258702/error-domain-nsurlerrordomain-code-1003-a-server-with-the-specified-hostname-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!