Shared Preferences in Objective C from Android -
is there anyway re-create code objective c? starting out, , need help.
string name = #;// vary. sharedpreferences userdata = this.getsharedpreferences(name + "userdata", mode_private); editor edit = userdata.edit(); edit.clear(); edit.putfloat("rating", rating.getrating()); edit.putstring("good", txtgood.gettext().tostring().trim()); edit.putstring("improve", txtimprove.gettext().tostring().trim()); edit.commit(); log.d(tag, "saving data");
the advantage of code is makes new sharedpreference everytime method called. experience using nsuserdefaults, able make 1 batch of data.
- (nsstring*)getfilepath:(nsstring*)filename { nserror *error; nsarray *paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes); //1 nsstring *documentsdirectory = [paths objectatindex:0]; //2 nsstring* file = [[nsstring alloc]initwithformat:@"%@.plist",filename]; nsstring* path = [documentsdirectory stringbyappendingpathcomponent:file]; nsfilemanager *filemanager = [nsfilemanager defaultmanager]; if (![filemanager fileexistsatpath: path]) //4 { nsstring *bundle = [[nsbundle mainbundle] pathforresource:filename oftype:@"plist"]; //5 [filemanager copyitematpath:bundle topath:path error:&error]; //6 } return path; }
and call method:
nsmutabledictionary* tempdict = [[nsmutabledictionary alloc]initwithcontentsoffile:[self getfilepath:@"filename"]];
plist sharedpreferences can save anykind of data. arrays,strings,booleans etc , there no limit that. can take @ this more info.
Comments
Post a Comment