Important Fact
I forgot to mention an important factor in the question. I am running this in a TestCase. I think this issue has something to do with the TestCase not awaiting for async completionHandler to return
Migrated out from Alamofire
to SwiftHTTP
, since I found this much easier.
On SwiftHHTP there is no way to know what URL got generated, what error it returned. For example, I tried to see the opt.debugDescription
variable, it returned something cryptic like description String "<SwiftHTTP.HTTP: 0x60000007e540>"
Steps I have followed
- I have set
YES
toAllow Arbitrary Loads
. - Safari on the iPhone Simulator responds with the correct JSON if I paste
fullurl ->http://120.0.0.1:8080/myapi/Driver/getDriver?driver=2243&domain=4345&key=asdfasdf
. Evencatalina.out
on the tomcat server running on my mac responds with a debug message.
But when I run this in a test case under Xcode the below code prints none of debug print's.
- --1->, --2-->, --3-->, nothing got printed.
- Debugger breakpoints also dont stop here.
CODE
var getData = [String:String]()
getData = ["domain": "4345",
"driver" : "2343",
"key" : "asdfasdf"]
var urlComponents = URLComponents(string: fullURL)!
var queryItems = [URLQueryItem]()
queryItems = self.getData.map{ URLQueryItem(name : $0.0, value : $0.1) }
urlComponents.queryItems = queryItems
print("fullurl ->"+(urlComponents.url)!.absoluteString)
do {
let opt = try HTTP.GET((urlComponents.url)!.absoluteString)
opt.start { response in
if let err = response.error {
print("--1-> error: \(err.localizedDescription)")
return //also notify app of failure as needed
}
print("--2--> opt finished: \(response.description)")
self.responseData = response
}
} catch let error {
print("--3--> got an error creating the request: \(error)")
}
EDIT
Even after changing the code to https
or http://www.google.com
, same result.
let testComponents = URLComponents(string: "https://www.google.com")!
URLSession.shared.dataTask(with: (testComponents.url)!, completionHandler: {
(data, response, error) in
if(error != nil){
print("..1>..")
}else{
do{
print ("..2>.." )
let json = try JSONSerialization.jsonObject(with: data!, options:.allowFragments) as! [String : AnyObject]
self.responseData = json
}catch let error as NSError{
print("..3>..")
}
}
}).resume()
EDIT 1 Tried from here @Vivek's answer.
callWebService(url: (urlComponents.url)!.absoluteString)
.
.
func callWebService(url : String) {
.
.
let callURL = URL.init(string: url)
Nothing got printed again, Error / JSON, nothing.
printing
too – Trinitrobenzenesetenv("CFNETWORK_DIAGNOSTICS", "3", 1);
does not enable any extra log files – Trinitrobenzene