How to run timer though the app entered background or is terminated

前端 未结 3 1911
心在旅途
心在旅途 2020-12-07 06:24

In my app I have a NSTimer, and a selector - (void)TimerCount. I also have a integer int CountNum, and every 0.01 second, it increases

3条回答
  •  有刺的猬
    2020-12-07 06:42

    You can use NSuserdefaults to save your countNum, whenever user will quit the app it will be saved. Just call this into APPDELEGATE function - (void)applicationWillTerminate:(UIApplication *)application {

    NSUserDefault *defaults = [NSUser Defaults standardUserDefaults];
    [defaults setInteger: countNum forKeys:@"ANY_KEY_HERE"];
    [defaults synchronise];
    

    the above code will save your countNum, but you have to save it when user will terminate or go into background.

    And When User will come into foreground you can call to retrive your countNum

    NSUserDefault *defaults = [NSUser Defaults standardUserDefaults];
    yourCountNum = [default getIntegerForKeys: @"ANY_KEY_HERE"];
    

提交回复
热议问题