iOS 6 - 状态保存和恢复

时间:2012-09-24 14:54:43

标签: iphone xcode cocoa-touch ios6 uikit-state-preservation

我已经实现了用于状态保存的iOS 6 API,它可以工作 - 在我退出应用程序并重新启动几毫秒后,恢复的视图控制器会进入,但之后它被我在启动时显示的主视图控制器所取代。 / p>

每次应用程序启动主窗口的根视图时我都会设置,所以这一定是问题所在。

这是我的代码:

- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self commonInitializationLaunching:launchOptions];
    return YES;
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [self commonInitializationLaunching:launchOptions];
    return YES;
}

- (void)commonInitializationLaunching:(NSDictionary *)launchOptions
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{

        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        static NSString *const kKeychainItemName = @"OAuthGoogleReader";
        self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
        self.navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];

        GTMOAuth2Authentication *auth;
        auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName
                                                                     clientID:kClientID
                                                                 clientSecret:kClientSecret];

        self.window.rootViewController = self.navController;

        [self.window makeKeyAndVisible];

        BOOL isSignedIn = [auth canAuthorize];
        if (isSignedIn) {
            NSLog(@"Signed");
        }else{
            NSString *scope = @"https://www.google.com/reader/api/";

            GTMOAuth2ViewControllerTouch *viewController;
            viewController = [[GTMOAuth2ViewControllerTouch alloc] initWithScope:scope
                                                                        clientID:kClientID
                                                                    clientSecret:kClientSecret
                                                                keychainItemName:kKeychainItemName
                                                                        delegate:self
                                                                finishedSelector:@selector(viewController:finishedWithAuth:error:)];
            [self.navController pushViewController:viewController animated:YES];
            //        self.window.rootViewController = viewController;
        }
    });
}

你可以看到 - (void)commonInitializationLaunching:(NSDictionary *)launchOptions 我正在设置窗口的根视图。我不知道该放什么。也许检查是否有保存状态,然后加载此方法?但是如何?

谢谢!

以下是我按照Rob的建议尝试的内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (!self.isRestored) {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    }
    [self commonInitializationLaunching:launchOptions];
    [self.window makeKeyAndVisible];
    return YES;
}

willFinishLaunching中没有任何内容...... 我还通过我的commonInitializationLaunching方法的窗口代码删除了。

2 个答案:

答案 0 :(得分:24)

故事板将为您完成大部分繁重工作,例如恢复窗口。但是,使用代码不会恢复窗口。您需要使用编码器保持根视图控制器。您的代码将如下所示:

NSString * const AppDelegateRootVCKey = @"AppDelegateRootVCKey";

- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder {
    [coder encodeObject:self.window.rootViewController forKey:AppDelegateRootVCKey];
}

- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder {

    // Grabs the preserved root view controller.
    UIViewController * vc = [coder decodeObjectForKey:AppDelegateRootVCKey];

    if (vc) {
        UIWindow * window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        window.rootViewController = vc;
        window.restorationIdentifier = NSStringFromClass([window class]);

        // The green color is just to make it obvious if our view didn't load properly.
        // It can be removed when you are finished debugging.
        window.backgroundColor = [UIColor greenColor];

        self.window = window;
    }
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    if (!self.window) {

        UIWindow *window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // The blue color is just to make it obvious if our view didn't load properly.
        // It can be removed when you are finished debugging.
        window.backgroundColor = [UIColor blueColor];

        UIViewController *root = // However you create your root.

        window.rootViewController = root;
        window.restorationIdentifier = NSStringFromClass([window class]);

        self.window = window;
    }

    [self commonInitializationLaunching:launchOptions];
    [self.window makeKeyAndVisible];

    return YES;
}

需要注意的另一个问题是确保UINavigationControllerUITabBarController具有恢复标识符。

答案 1 :(得分:6)

状态恢复通常与故事板集成在一起。如果您正在使用故事板,则不应创建自己的窗口,查看控制器等。您应该让故事板为您执行此操作。发生的事情是故事板正在进行所有状态恢复,然后你正在创建一个新窗口并将其置于所有状态之上。如果是这种情况,您可能会在每次启动时创建两个UI副本。你只是没有注意到它。


如果您在代码中构建整个界面(不是推荐的方法,但确实有效),那么您需要确定在创建UI之前是否发生了状态恢复。这很简单:

  • commonInitializationLaunching:中,仅初始化非UI元素(不会出现在状态保存中的事物)。这是处理UI元素在状态恢复期间可能依赖的事物的地方。您当前的代码中没有任何这些。

  • application:didDecodeRestorableState:中,设置一个app delegate ivar以指示状态已恢复。

  • application:didFinishLaunchingWithOptions:中,在运行commonInitializationLaunching:后,检查您的ivar。如果未恢复状态,请创建UI。

请记住commonInitializationLaunching:模式仅用于向后兼容iOS 5.如果您不需要,只需将非UI放入willFinish和UI didFinish (如果状态未恢复)。