ios 状态恢复怎么弄
阿神
阿神 2017-04-17 17:41:05
0
1
291

1.看来了好几个例子,还是没搞清楚,每次app第一次运行都会 Warning: Unable to create restoration in progress marker file,并且第一次不会进入decodeRestorableStateWithCoder方法
2.这个不是我想实现的效果,我想实现每次程序后台和关闭的时候自动保存,然后程序启动的时候恢复,第一次也能正常

阿神
阿神

闭关修行中......

全部回覆(1)
小葫芦
  1. 狀態恢復功能在模擬器上不可用,不好測試,真機沒有問題,搞機吧,少年。

  2. 使用NSUserDefaults替代:

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        //load status when starting
        NSString * statusToRestore = [[NSUserDefaults standardUserDefaults] objectForKey:@"FightingStatus"];
        NSDate *lastUpdated=[[NSUserDefaults standardUserDefaults] objectForKey:@"LastUpdated"];
        NSLog(@"restored:%@ on %@",statusToRestore,lastUpdated);
        return YES;
    
    }
    
    -(void)applicationWillEnterForeground:(UIApplication *)application {
        //restore status when reactive
        NSString * statusToRestore = [[NSUserDefaults standardUserDefaults] objectForKey:@"FightingStatus"];
        NSDate *lastUpdated=[[NSUserDefaults standardUserDefaults] objectForKey:@"LastUpdated"];
        NSLog(@"restored:%@ on %@",statusToRestore,lastUpdated);
    }
    
    -(BOOL)application:(UIApplication *)application shouldSaveApplicationState:(nonnull NSCoder *)coder
    {
        return YES;
    }

    -(BOOL) application:(UIApplication *) application shouldRestoreApplicationState:(nonnull NSCoder *)coder
    {
        return YES;
    }

    -(void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(nonnull NSCoder *)coder
    {
        //
        //[coder encodeFloat:2.0f forKey:@"Version"];
        //NSLog(@"encode version :2.0f");
        
        //save status when inactive
        NSString * statusToSave=@"Fighting";
        NSDate *lastUpdated = [NSDate dateWithTimeIntervalSinceNow:-3600];
        [[NSUserDefaults standardUserDefaults] setObject:statusToSave forKey:@"FightingStatus"];
        [[NSUserDefaults standardUserDefaults] setObject:lastUpdated forKey:@"LastUpdated"];
        NSLog(@"saved:%@ on %@",statusToSave,lastUpdated);
    }
    
    -(void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder
    {
        //failed when testing in simulator
        //NSLog(@"Version=%g",[coder decodeFloatForKey:@"Version"]);
    }
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板