2

I am developing a Command Line Tool in Swift 3, i have this code:

let url = "www.google.com"
var request = URLRequest(url:url)
request.httpMethod = "GET"

let task = session.dataTask(with: a){ (data,response,error) in

        print("REACHED")
        handler(response,data)


}
task.resume()

I cannot reach the task if i use "http://" or "https://" as prefix in any url, i am wondering if i need a App Transport Security plist, i already tried create a simple plist, anyone knows some if has a particularity for this problem?

eduardo
  • 123
  • 1
  • 11

1 Answers1

1
  1. When specifying a URL, you need the "scheme" (e.g. http:// or https://).

  2. The url is a URL, not a String, so it should be:

    let url = URL(string: "http://www.google.com")!
    
  3. Yes, you need Info.plist entry if you want to use http://. E.g. https://stackoverflow.com/a/37552442/1271826 or Transport security has blocked a cleartext HTTP or just search stack overflow for "[ios] http info.plist" or with [osx].

    Note, in Xcode 8.1, console apps don't necessarily have a Info.plist file, so if you don't have one, you may have to add one by pressing command+n:

    add plist

    update your target settings to specify the plist:

    plist target settings

    and then add the appropriate settings, e.g.:

    enter image description here

  4. I assume where you have URLRequest(with: a) you meant URLRequest(with: request).

  5. You'll need something to keep the command app alive while you're performing the request (e.g. a semaphore or something like that).

Community
  • 1
  • 1
Rob
  • 415,655
  • 72
  • 787
  • 1,044
  • Rob, thank you for your help, i'm using a plist now, but i still have request problems, i assume that problems is about the semaphore, but i'm using OperationQueue and BlockOperation to make multiple requests, i use that function to wait operationQueue.waitUntilAllOperationsAreFinished(), but i can't make requests, when i use HTTP or HTTPS, the operations doesn't work, can you help me again? Thanks – eduardo Nov 11 '16 at 21:22
  • Okay, Rob, thank you, i will subclass the operation! – eduardo Nov 11 '16 at 23:04