15

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

FelixSFD
  • 6,052
  • 10
  • 43
  • 117
shriniwas_ayyer
  • 161
  • 1
  • 1
  • 4

3 Answers3

21

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
sarfata
  • 4,625
  • 4
  • 28
  • 36
12

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.

JoGoFo
  • 1,928
  • 14
  • 31
  • 1
    Sweet ... I had a similar issue and it turned out that my endpoint was not reachable on my wifi network (i.e. physical device) but was on my hardwired laptop (i.e. iOS Simulator). I had to use a different secure in-house wifi network. – Chris Allinson Jul 27 '15 at 16:34
  • Thanks man. You are awesome. I had same problem and i was checking safari for internet connectivity. Every time it showed Google page and i felt i had working internet connection. After reading your answer, I checked opening another pages and found that Goggle page was cached one. +1 – Mujib Saiyyed Sep 24 '15 at 06:58
  • i just can't understand that in Android it will work fine with the network but in ios not. what can i exactly do for solve this issue? – ios developer Jul 20 '16 at 07:53
  • 3
    Hey is problem solved for you? I have a similar issue I tried a GET request in Safari and its work fine but within my app I get the same error – Dania Delbani Sep 11 '16 at 04:47
  • 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 (comment also added here: https://stackoverflow.com/q/42996709/1084192) – salzo Apr 24 '23 at 05:31
5

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.

Baskar
  • 61
  • 1
  • 10