13

I am trying to get rest data to iOS app, and I use:

var rest_url = "http://192.168.0.1:8000/rest/users/"

let url: NSURL = NSURL(string: rest_url)
let session = NSURLSession.sharedSession()
let task = session.dataTaskWithURL(url, completionHandler: {data, response, error -> Void in
if(error != nil) {
    println(error.localizedDescription)
}
println(data)
var err: NSError?

var jsonResult = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: &err) as NSDictionary!

But I think I can't access my server like this. Does anyone know how I can access my server from the iOS simulator?

Pang
  • 9,564
  • 146
  • 81
  • 122
Mirza Delic
  • 4,119
  • 12
  • 55
  • 86
  • 2
    Localhost is `127.0.0.1`. Your webserver is probably listening on that IP only. – B.R.W. Sep 06 '14 at 16:34
  • `The operation couldn’t be completed. (NSURLErrorDomain error -1004.) <> JSON Error The operation couldn’t be completed. (Cocoa error 3840.) fatal error: unexpectedly found nil while unwrapping an Optional value` – Mirza Delic Sep 06 '14 at 17:06

5 Answers5

7

Do you have App Transport Security Settings in your Info.plist file? If no then for debugging purpose you can set them like

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>

Such settings allow issuing requests to any server. But don't do so for a release version. It is insecure.

Dmitry A.
  • 588
  • 8
  • 13
6

Make sure that your IP of your Mac is 192.168.0.1. So your url could be

var rest_url = "http://YOUR MAC IP:8000/rest/users/"
SFeng
  • 2,226
  • 1
  • 19
  • 16
  • i run python server on 192.168.0.13 with port 8000, i tryed with that url, and with 127.0.0.1:8000, but nothing works :/ `The operation couldn’t be completed. (NSURLErrorDomain error -1004.) <> JSON Error The operation couldn’t be completed. (Cocoa error 3840.) fatal error: unexpectedly found nil while unwrapping an Optional value` – Mirza Delic Sep 06 '14 at 17:06
4

If you have a server running on the machine where you iOS simulator is running, then you need to choose 'http://127.0.0.1' as the URL.

In your case it will be :

var rest_url = "http://127.0.0.1:8000/rest/users/"
Navid
  • 384
  • 2
  • 7
3

For people finding this thread because they can't connect to localhost due to an invalid certificate: in your URLSessionDelegate you should respond to the URLAuthenticationChallenge with the following delegate method:

func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {

    func defaultAction() { completionHandler(.performDefaultHandling, nil) }

    // Due to localhost using an invalid certificate, we need to manually accept it and move on
    guard challenge.protectionSpace.host.hasPrefix("localhost") else { return defaultAction() }
    guard challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust else { return defaultAction() }
    guard let trust = challenge.protectionSpace.serverTrust else { return defaultAction() }

    completionHandler(.useCredential, URLCredential(trust: trust))
}
Max Chuquimia
  • 7,494
  • 2
  • 40
  • 59
1

Maybe you can replace 192.168.0.1 with localhost, when debugging with iOS simulator (that is, real devices should use your server's IP).

I also cannot access my test server using IP address on simulator. But when I am using localhost or 120.0.0.1, the simulator can work well with my test server.

Pang
  • 9,564
  • 146
  • 81
  • 122
llch
  • 129
  • 1
  • 3
  • 10