36

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)!)
        }
    }

}
Async-
  • 3,140
  • 4
  • 27
  • 49
  • @vadian No it isn't. Have you read the other question? – JeremyP Mar 24 '17 at 10:24
  • 1
    I'm assuming Swift 3 on iOS 10... have you tried temporarily replacing `getDataFromUrl(...)` with `UIApplication.shared.open(url, options: [:], completionHandler: nil)` just to see if it properly launches Safari and connects to the URL /(retrieves the image) correctly? – DonMag Mar 24 '17 at 12:38
  • For Xcode 11 :- https://stackoverflow.com/a/58833674/4933696 – Anurag Sharma Apr 14 '20 at 11:35
  • This can also be misleadingly reported because of an URL doing a redirect (e.g. `HTTP 30X` with a `Location` header) and the URL to be redirected to being the one "not foundable". In that case, iOS reports the initial original URL, not the one having the issue. In React Native with `WebView`, the [onShouldStartLoadWithRequest](https://github.com/react-native-webview/react-native-webview/blob/master/docs/Reference.md#onshouldstartloadwithrequest) method can be used to debug this. – salzo Apr 24 '23 at 05:28

5 Answers5

89

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.

enter image description here

Udaya Sri
  • 2,372
  • 2
  • 25
  • 35
  • 18
    Unfortunately the error is very misleading here. It isn't that DNS isn't working, it's that the app might not have permission to access the network at all! Simply selecting "Outgoing Connections (Client)" will allow you to make network requests. – spfursich May 04 '18 at 17:27
  • 4
    Awesome, this just happened with me in a macOS app. – adev Jun 14 '18 at 00:04
  • 2
    I have lost two days on this, it didn't make any sense at all! Thanks, you saved me hours of headaches. – Michele Dall'Agata Jul 19 '18 at 09:46
  • 1
    Better: set the flag for incoming/outgoing connections (see screenshot above). – de. Jul 31 '19 at 14:07
48

Slight improvement over Udaya Sri's answer:

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

enter image description here

de.
  • 7,068
  • 3
  • 40
  • 69
  • this works but if you go to Capabilities you might not see App Sandbox there. I had to follow this answer on where to find it and to get it to appear. After you follow the steps it will take 5 - 10 minutes before it appears: https://stackoverflow.com/a/58833674/4833705 – Lance Samaria May 17 '20 at 13:47
  • 1
    i cannot see this in Xcode 12.4, can anyone please suggest with the steps? Thanks in advance – Arshad Shaik Aug 20 '21 at 04:33
  • @Arshad Shaik select Blue Project Icon on the left > signing & capabilities > scroll down to App sandbox (if missing: plus icon to add capability) – de. Aug 20 '21 at 06:21
  • 1
    @de. it didn't work, need to add "App Sandbox" in ".entitlements" file and set to "YES" and will see it in capabilities – Arshad Shaik Aug 20 '21 at 09:15
3

This happened in my case because one of the paths in our development servers was 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.

bshirley
  • 8,217
  • 1
  • 37
  • 43
2

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.

Sreejith S
  • 376
  • 2
  • 12
1

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.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • 1
    man, it's not, the full URL is correctly made, and I can see it opens in the safari browser. It's not about the URL, URL is correct – Async- Mar 24 '17 at 10:30
  • I am seeing the URL as constructed by the program at runtime, and also output to the log as well as part of the error, sorry I can't put URL here, but it's properly constructed – Async- Mar 24 '17 at 11:32
  • in my case the base URL is not just a website address, it's a proper URL of the API ending with "/" so adding email to it is correct, and the URL it outputs at the runtime is correct, it looks like the app can't access it – Async- Mar 24 '17 at 11:38
  • @JeremyP: Can you please let me know if there are different error codes for DNS resolution failure/ timeout and for invalid host names. I am facing similar issue, I want to understand if it's a host name error (or) if its a DNS issue – Vivek Maran Sep 11 '17 at 16:11