iOS Error Code=-1003 “A server with the specified hostname could not be found.”

烈酒焚心 提交于 2019-12-04 10:21:14

问题


I am trying to load image from URL on the iphone, image is there and I can open it in safari with same link, but not in the app:

Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname 
could not be found." UserInfo={NSUnderlyingError=0x17024f810 
{Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" 
UserInfo={_kCFStreamErrorCodeKey=50331647, _kCFStreamErrorDomainKey=6147928288}},
 NSErrorFailingURLStringKey=https://........, NSErrorFailingURLKey=https://.........

Code of request:

func downloadImage(userEmail: String, onCompletion: @escaping (UIImage) -> Void) {
    print("Download Started")
    let route = "\(baseURL as String)\(userEmail as String)\(baseURLparameters as String)"
    let url = URL(string: route)

    getDataFromUrl(url: url!) { (data, response, error)  in
        guard let data = data, error == nil else {
            print("===failed:", error ?? "dunno")
            print("===url:", url?.absoluteString ?? "dunno")
            return
        }
        print(response?.suggestedFilename ?? url!.lastPathComponent )
        print("Download Finished")
        DispatchQueue.main.async() { () -> Void in
            onCompletion(UIImage(data: data)!)
        }
    }

}

回答1:


In my case, when I connect the signing team account to Xcode it was automatically switch on the App Sandbox in Capabilities. When I turn off it I could do server request without any problems.




回答2:


The host name in your URL is wrong because the message you are getting back explicitly states it can't find a server with that host name.

The mst likely cause is that you have forgotten to put a forward slash between the base URL and the email e.g. if your base URL is

http://example.com

and your email is

jeremyp@example.com

You construct

http://example.comjeremyp@example.com

Put a break point on the line that creates the URL and inspect the string it is trying to create it from. It'll probably then be pretty obvious exactly what you are doing wrong.




回答3:


This error would suggest as DNS related issue.There is some problem with DNS server reached through our WiFi network. So we change DNS server and fix this problem. Remove cache and try again.

If there is a url you can GET (instead POST) on the same server.

And Also, turning off wifi and using 3G makes the error go away.




回答4:


Slight improvement over Udaya Sri's answer:

Explicitly allow for network connections while keeping the sandbox enabled like so:




回答5:


This happened in my case because one of the paths in our development servers were internal to our firewall/intranet/VPN and my device was not on our internal wi-fi.

Frustrating and confusing because it was working in the simulator and not on a device.



来源:https://stackoverflow.com/questions/42996709/ios-error-code-1003-a-server-with-the-specified-hostname-could-not-be-found

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