ios - Store a value for the app's running time -
i need store boolean value persist app's running time. when user quit app (from background well) variable should reset default. how can this?
i tried constants. keep constants in separate .h file. in declared this.
const bool hasshowntutorial = no;
and in view controller,
if (hasshowntutorial == no) { [self showapptutorial]; hasshowntutorial = yes; }
i error @ hasshowntutorial = yes;
saying read-only variable not assignable.
i tried going using nsuserdefaults
. thing value stored once set it. there way clear when app quits?
i'd appreciate input , suggestions.
thank you.
store in nsuserdefaults
under specific key.
[[nsuserdefaults standarddefaults] setbool:<mybool> forkey:@"mykey"];
then retrieve later.
bool b = [[nsuserdefaults standarddefaults] boolforkey:@"mykey"];
if want clear data.
nsstring *domain = [[nsbundle mainbundle] bundleidentifier]; [[nsuserdefaults standarduserdefaults] removepersistentdomainforname:domain];
for example, in appdelegate
's implementation of -applicationwillterminate:
, clear data.
- (void)applicationwillterminate:(uiapplication *)application { nsstring *domain = [[nsbundle mainbundle] bundleidentifier]; [[nsuserdefaults standarduserdefaults] removepersistentdomainforname:domain]; }
edit: if first method of clearing data not work, can use class method +resetstandarduserdefaults
. example following clear current defaults.
[nsuserdefaults resetstandarduserdefaults];
a third possiblity remove property.
[[nsuserdefaults standarduserdefaults] removeobjectforkey:@"mykey"];
Comments
Post a Comment