Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ios simulator access localhost server

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

    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 that i can't access my server like this, anyone know how can i access my server from ios simulator?

like image 764
Mirza Delic Avatar asked Sep 06 '14 16:09

Mirza Delic


2 Answers

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.

like image 85
Dmitry A. Avatar answered Oct 02 '22 21:10

Dmitry A.


May be 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.

like image 23
llch Avatar answered Oct 02 '22 21:10

llch