iphone - Read/Write to a plist file that comes bundled with the app -
iphone - Read/Write to a plist file that comes bundled with the app -
i building add-on app user can search item in list pre-populated info .plist file. nsdictionary. if term, user searched for, not exist, user can tap + button , add together there next time.
first of thought easy using nsuserdefaults, few problems arises.
to have list included must place in bundle, if there can not add together new key/value pairs it. can files situated in documents folder.
so guess have bundle plist, on first run i'll move documents folder , access there.
this opens problem when need update app, guess overwrite values user set in.
is there secure, easy-understandable, right way accomplish functionality describe?
thanks help given:)
edit: **** actual approach, suggested thesquad , tomh *****
+ (nsmutabledictionary*) genericproducts { nsfilemanager *filemanager = [nsfilemanager defaultmanager]; nsarray *documentpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [documentpaths objectatindex:0]; nsstring *documentplistpath = [documentsdirectory stringbyappendingpathcomponent:@"genericproducts.plist"]; nsstring *bundlepath = [[nsbundle mainbundle] bundlepath]; nsstring *bundleplistpath = [bundlepath stringbyappendingpathcomponent:@"genericproducts.plist"]; if([filemanager fileexistsatpath:documentplistpath]){ nsmutabledictionary *documentdict = [nsmutabledictionary dictionarywithcontentsoffile:documentplistpath]; homecoming documentdict; } else { nserror *error; bool success = [filemanager copyitematpath:bundleplistpath topath:documentplistpath error:&error]; if (success) { nsmutabledictionary *newlysaveddict = [nsmutabledictionary dictionarywithcontentsoffile:documentplistpath]; homecoming newlysaveddict; } homecoming nil; }
}
and adding new product list:
+ (void) additemtogenericproducts:(nsstring*) newproduct { nsarray *documentpaths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); nsstring *documentsdirectory = [documentpaths objectatindex:0]; nsstring *documentplistpath = [documentsdirectory stringbyappendingpathcomponent:@"genericproducts.plist"]; nsmutabledictionary *documentdict = [nsmutabledictionary dictionarywithcontentsoffile:documentplistpath]; [documentdict setobject:newproduct forkey:[md5checksum cheksum:newproduct]]; [documentdict writetofile:documentplistpath atomically:yes];
}
i had same thoughts sqlite database...
i end doing that, re-create bundled file documents in order able modify it.
what have done checking @ each startup if file exist, if not, re-create it. if update of app, documents folder not touch, means copied file previous version still present.
the issue if want plist upgraded have handle in application. if have suggest utilize nsuserdefault check if previous version of app existed before...
iphone plist nsdictionary
Comments
Post a Comment