11

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.

Community
  • 1
  • 1
Siddharth
  • 9,349
  • 16
  • 86
  • 148

3 Answers3

3

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.

Siddharth
  • 9,349
  • 16
  • 86
  • 148
Magnas
  • 3,832
  • 5
  • 33
  • 48
  • Did not work as yet. I added permissions to info.plist. – Siddharth Feb 25 '17 at 19:02
  • Have you seen this? http://stackoverflow.com/questions/31254725/transport-security-has-blocked-a-cleartext-http – Magnas Feb 25 '17 at 19:28
  • I'll look at http://stackoverflow.com/questions/39053593/swift-3-urlsession-datatask-completionhandler-not-called tomorrow. – Siddharth Feb 25 '17 at 19:32
  • Adding `NSALLOWSArbitaryLoads` did not work, I still cannot see tomcat catalina.out logs from this Xcode test case – Siddharth Feb 27 '17 at 05:07
2

Put this in your AppDelegate:

setenv("CFNETWORK_DIAGNOSTICS", "3", 1);

Like described here https://developer.apple.com/library/content/qa/qa1887/_index.html

This should log out something like "The network log file is in file: x" and you can grep the file for your URL and check if there are any logged errors there that did not show up in your app. This is a very good way of debugging ATS or other network connectivity problems, as it is very verbose.

Alistra
  • 5,177
  • 2
  • 30
  • 42
  • I think this is what I am looking for. So you are saying that the async http response will continue to run and log into a network log file for offline analysis. Great!! I added this line to AppDelegate, but I dont know where to find the network log file. All I can see is the "Test session log" – Siddharth Feb 27 '17 at 13:15
  • I think I should find that file in CrashReporter folder under `Logs`. I am unable to find that folder. – Siddharth Feb 27 '17 at 13:21
  • It should be one of the first things it logs to the console when you set it. – Alistra Feb 27 '17 at 16:52
  • I was running it as a test case, not as a app. Now when I run it as an app everything starts to work. – Siddharth Feb 28 '17 at 04:23
  • Please post the whole method in which this is run – Alistra Feb 28 '17 at 08:13
1

Running network calls under a test case, needs the framework of XCTestExpectation.

Test case does not wait for async completionHandlers to be called. Unfortunately XCTestExpectation framework is not design friendly. We should be able to integrate test cases without having to change our class structure.

Lets track the responses to this on the Apple Forum

Siddharth
  • 9,349
  • 16
  • 86
  • 148