objective c - Downloading an XML file on an iPhone, storing it and then using it -
i coding application using xml document retrieve data (i using libxml2.2.7.3). set load local xml-file (in project in xcode, along other project files). found wanted able edit xml file , have instant updates on application. fetch data xml document this:
nsarray *dagensretlist = [self getallitems:@"//dagensret" filename:@"dagensret.xml"];
i thought easiest way come around download xml-file whenever there new version of document available webserver provide (on every application launch / clicking refresh button download new file server - maybe let check whether have same tag (weeknumber, it's danish coded application))
so thinking on convenient way download file , should keep way of fetching data or more wise keep document on server , read directly server everytime? (it end using lot of traffic, it's product school user-base around 1200, less not using smartphone)
how download file webserver , keep cached?
you should cache file on device in case user can't connect server.
something should started:
// create url request , set url nsurl *url = [nsurl urlwithstring:@"http://google.com"] nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:url]; // display network activity indicator [[uiapplication sharedapplication] setnetworkactivityindicatorvisible:yes]; // perform request on new thread don't block ui dispatch_queue_t downloadqueue = dispatch_queue_create("download queue", null); dispatch_async(downloadqueue, ^{ nserror* err = nil; nshttpurlresponse* rsp = nil; // perform request synchronously on thread nsdata *rspdata = [nsurlconnection sendsynchronousrequest:request returningresponse:&rsp error:&err]; // once response received, handle on main thread in case ui updates dispatch_async(dispatch_get_main_queue(), ^{ // hide network activity indicator [[uiapplication sharedapplication] setnetworkactivityindicatorvisible:no]; if (rspdata == nil || (err != nil && [err code] != noerr)) { // if there no data received, or error... } else { // cache file in cache directory nsarray* paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes); nsstring* path = [[paths objectatindex:0] stringbyappendingpathcomponent:@"init.xml"]; [[nsfilemanager defaultmanager] removeitematpath:path error:nil]; [data writetofile:path atomically:yes]; // whatever else want data... } }); });
Comments
Post a Comment