I hit a web service in swift 3.0 and I am getting response in offline mode also.
Here is the details of my Work:
I need to call 1 web service - let url = URL(string: "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors")
By using URLSession.shared.dataTask(...) method I called above web service.
By using try? statement ' JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.mutableContainers) as AnyObject ' I parsed the data.
I ran above code and it gives me response what I am expected (Online Mode).
Now I turn off my wifi & run the above code again then I again got the same data as that in online. I put debugger on completionHandler closure on success part then I reached at that breakpoint.
Please I need help. Why I am getting response in offline mode (I print array size & it shows size = 6)?
Here is the code.
I am working on Xcode 8.1 with Swift 3.0
{ final let urlString = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors"
override func viewDidLoad() { super.viewDidLoad()
self.downloadJsonWithURL()
print("Inside viewDidLoad")
// Do any additional setup after loading the view, typically from a nib.
}
func downloadJsonWithURL() {
let url = URL(string: urlString)
let task = URLSession.shared.dataTask(with: url!, completionHandler: {(data, response, error) -> Void in
if error != nil
{
print("Error Found!!!!")
return
}
else
{
guard let jsonObj = try? JSONSerialization.jsonObject(with: data!, options:JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
else
{
// if 'let jsonObj = try? JSONSerialization.jsonObject(with: data!, options: []) as? NSDictionary' fails(i.e falls) then this block will execute.
return
}
if let actorArray = jsonObj.value(forKey: "actors") as? NSArray {
for actor in actorArray{
print("Actor Array = \(actorArray.count)")
if let actorDict = actor as? NSDictionary {
if let name = actorDict.value(forKey: "name") {
self.nameArray.append(name as! String)
}
if let name = actorDict.value(forKey: "dob") {
self.dobArray.append(name as! String)
}
if let name = actorDict.value(forKey: "image") {
self.imgURLArray.append(name as! String)
let data = NSData(contentsOf: URL(string: name as! String)!)
self.imgArray.append(UIImage(data: data as! Data)!)
}
}
}
OperationQueue.main.addOperation({
self.tableView.delegate = self as? UITableViewDelegate
self.tableView.dataSource = self
self.tableView.reloadData()
})
}
}
})
task.resume()
}
}