I have an iOS application where I get my info from the servers, the problem is that none of the requests I am using is working because the code execution is exiting the code, before getting the response back from the server, or maybe it is failing, anyways here is what I have tried so far:
let manager = AFHTTPSessionManager()
manager.post("www.myapps.com/api/Files/getFiles?folderId=6", parameters: nil, progress: nil, success: { (task: URLSessionTask, responseObject) in
if let response = responseObject as? [String:Any],
let medias = response["medias"] as? [String:Any] {
locked = false
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
let url = NSURL(fileURLWithPath: documentsPath)
let contentFile = url.appendingPathComponent("Content.json")
if let data = try? JSONSerialization.data(withJSONObject: medias) {
try? data.write(to: contentFile!, options: .atomic)
}
let data = try! Data(contentsOf: contentFile!)
self.jsonData = JSON(data:data)
self.categoriesCount = self.jsonData["categories"].count
print(self.categoriesCount)
}
},failure: { (operation: URLSessionTask!, NSError) in
print(NSError.localizedDescription)
})
while locked {
wait()
}
}
func wait()
{
DispatchQueue.main.async {
RunLoop.current.run(mode: RunLoopMode.defaultRunLoopMode, before: NSDate(timeIntervalSinceNow: 1) as Date)
}
}
keep in mind that if I use my URL directly in my browser I do get a response which means it is working, but in my application, something is going wrong any ideas?