xNinjaSwift xNinjaSwift - 1 year ago 116
iOS Question

URLSession cached data

I'm new in iOS dev and I do not understand one think. So I have gz file and inside gzip there is xml file. I need to download gz file, every time user start the app. First time when I start my app I use this code to get data. Problem is my xml file was offline on server for few day and my app always start without problem and show data with no problem. So all my files was cached on device?. I want my data is retrieved every time while the user start the application. I am not sure do I did something wrong? Thanks

let url = NSURL(string: "http://sitename/xxx.gz")

if url != nil {
let task = URLSession.shared.dataTask(with: url! as URL, completionHandler: { (data, response, error) -> Void in

if error == nil {

let nsdata = data as NSData?
let content = nsdata?.gunzipped()
let dataContent = content as Data?

let urlContent = NSString(data: dataContent!, encoding: String.Encoding.ascii.rawValue) as NSString!

let xml = XMLParser()
xml.getDataforTable(data: urlContent as! String)

NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "XmlDataLoaded"), object: nil))

} else {
NotificationCenter.default.post(Notification(name: Notification.Name(rawValue: "DataNotLoaded"), object: nil))

Answer Source
enter code here 

in AppDelegate.swift

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

call your web services method inside this method


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download