Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS Simulator not sending http requests to localhost mac running tomcat webservice

Tags:

ios

swift

swift3

Important information, I am running all this under a TestCase.

My URL is http://my-MacBook-Pro.local:8080/myapi/v2/Driver/getDriver?domain=123&driver=125&key=9808098

If I paste this URL on Safari running on the same iPhone Simulator. It responds with the JSON. I noticed my catalina.out reported this request.

I have enabled Allow HTTP Services on Settings->Developer.

This is the code snippet

print("fullurl ->"+urlComponents.url!.absoluteString)

URLSession.shared.dataTask(with: urlComponents.url!, completionHandler: {
        (data, response, error) in
        if(error != nil){
            print("error")
        }else{
            do{
                let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
                print (json)

            }catch let error as NSError{
                print(error)
            }
        }
    }).resume()

EDIT

Reference Added the following to info.plist, still my http request did not get thru to my localhost mac running tomcat on port 8080

<dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>My-MacBook-Pro.local</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
                <key>NSIncludesSubdomains</key>
                <true/>
            </dict>
        </dict>
    </dict> 

EDIT

Tried even with these settings in info.plist. No impact. Did a Clean, yet no impact.

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

Something wrong with my code I guess.

like image 736
Siddharth Avatar asked Feb 25 '17 10:02

Siddharth


Video Answer


1 Answers

You need to set the appropriate flag in the info.plist file in the same way you do for asking to track user locations or to access the photo library and camera. Apple blocks, by default, requests to insecure web calls, i.e. http.

You can look up how to set individual exceptions or set the NSAllowsArbitraryLoads to true to allow ALL insecure calls. Naturally this is considered the easier but less safe option.

like image 74
Magnas Avatar answered Sep 29 '22 22:09

Magnas