iphone - how to access any method only once while launching the app for first time -
i have web service fetch data in json
form. calling service everytime application launching (inside appdidfinishlaunching
method) , storing in local file. want call service first time app launching , next time wanted use file in have stored json
data.
nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [paths objectatindex:0]; //live json data url nsurl *url = [nsurl urlwithstring:@"my url"]; nsdata *urldata = [nsdata datawithcontentsofurl:url]; //attempt download live data if (urldata) { nsstring *filepath = [nsstring stringwithformat:@"%@/%@", documentsdirectory,@"data.json"]; [urldata writetofile:filepath atomically:yes]; } //copy data initial package applications documents folder else { //file write nsstring *filepath = [nsstring stringwithformat:@"%@/%@", documentsdirectory,@"data.json"]; //file copy nsstring *json = [ [nsbundle mainbundle] pathforresource:@"data" oftype:@"json" indirectory:@"html/data" ]; nsdata *jsondata = [nsdata datawithcontentsoffile:json options:kniloptions error:nil]; //write file device [jsondata writetofile:filepath atomically:yes]; } nserror *jsonerror = nil; nsstring *jsonfilepath = [nsstring stringwithformat:@"%@/%@", documentsdirectory,@"data.json"]; nsdata *jsondata = [nsdata datawithcontentsoffile:jsonfilepath options:kniloptions error:&jsonerror ]; nsmutablearray *jsonarray = [nsjsonserialization jsonobjectwithdata:jsondata options:nsjsonreadingmutablecontainers error:&jsonerror]; self.serverresponsearray = jsonarray; nslog(@"%@",self.serverresponsearray);
by code writing json
response local file , storing in array use in whole app.
static nsstring * const somekey = @"havechecked"; - (void)applicationdidfinishlaunching:(nsnotification *)anotification { nsuserdefaults * stduserdefaults = [nsuserdefaults standarduserdefaults]; if(![[stduserdefaults objectforkey:somekey] boolvalue]) { [self trysomething]; } } -(void)gotjson { //async callback nsuserdefaults * stduserdefaults = [nsuserdefaults standarduserdefaults]; [stduserdefaults setbool:yes forkey:somekey]; [stduserdefaults synchronize]; }
it sounds need learn dealing files, here stack overflow reading:
what documents directory (nsdocumentdirectory)?
write file on ios
Comments
Post a Comment