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?
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With