ios simulator access localhost server

i0S Swift Issue

Question or problem in the Swift programming language:

I am trying to get rest data to ios app, and i use this code:

    var rest_url = ""

    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) {
    var err: NSError?

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

But i think that i can’t access my server like this, anyone know how can i access my server from ios simulator?

How to solve the problem:

Solution 1:

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


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

Solution 2:

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

var rest_url = "http://YOUR MAC IP:8000/rest/users/"

Solution 3:

If you have a server running on the machine where you iOS simulator is running, then you need to choose ‘‘ as the URL.

In your case it will be :

var rest_url = ""

Solution 4:

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

Solution 5:

May be you can replace 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, the simulator can work well with my test server.

Hope this helps!