iOS 状態の保存と復元

14

Click here to load reader

description

関東第61回Cocoa勉強会の発表資料。

Transcript of iOS 状態の保存と復元

Page 1: iOS 状態の保存と復元

状態の保存と復元2013.9.7

村上幸雄 @m_yukio

2013年 9月 8日 日曜日

Page 2: iOS 状態の保存と復元

•村上幸雄•@m_yukio•ビッツ有限会社http://www.bitz.co.jp/

2013年 9月 8日 日曜日

Page 3: iOS 状態の保存と復元

本日の内容

• State Preservation and Restoration

• iOS App Programming Guidehttps://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StatePreservation/StatePreservation.html#//apple_ref/doc/uid/TP40007072-CH11-SW13

• バックグラウンド実行時に停止させられた場合の状態の保存と復元を試す。

2013年 9月 8日 日曜日

Page 4: iOS 状態の保存と復元

サンプル

https://github.com/murakami/workbook/tree/master/ios/StateRestoration

2013年 9月 8日 日曜日

Page 5: iOS 状態の保存と復元

状態の保存と復元?• iOS 6から利用可能

• バックグラウンド実行時に停止させれ際に、停止前の状態を復帰させる

• 対象はビューコントローラとビュー。モデルはアプリで対応。

2013年 9月 8日 日曜日

Page 6: iOS 状態の保存と復元

ビューコントローラにIDをつけて、このIDで保存と復元をおこなう。

2013年 9月 8日 日曜日

Page 7: iOS 状態の保存と復元

ビューコントローラに独自の保存と復元のコードを追加する。

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder{ DBGMSG(@"DetailViewController: encodeRestorableStateWithCoder"); [super encodeRestorableStateWithCoder:coder]; [[Document sharedDocument] save]; [coder encodeInteger:self.detailItemIndex forKey:kUnsavedDetailItemIndexKey];}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder{ DBGMSG(@"DetailViewController: decodeRestorableStateWithCoder"); [super decodeRestorableStateWithCoder:coder]; self.detailItemIndex = [coder decodeIntegerForKey:kUnsavedDetailItemIndexKey]; DBGMSG(@"detailItemIndex:%d", self.detailItemIndex); [self configureView];}

2013年 9月 8日 日曜日

Page 8: iOS 状態の保存と復元

生成と破棄のタイミングに、状態の保存と復元のタイミングの考慮が加わる。

@implementation AppDelegate/* 状態情報復元前の初期化 */- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions: (NSDictionary *)launchOptions{ [[Document sharedDocument] load]; return YES;}/* 状態情報復元後の初期化 */- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions{ return YES;}/* 状態情報保存前のモデル保存 */- (void)applicationWillResignActive:(UIApplication *)application{ [[Document sharedDocument] save];}@end

2013年 9月 8日 日曜日

Page 9: iOS 状態の保存と復元

状態の保存と復元は、アプリケーションのデリゲーションで有効/無効にできる。

@implementation AppDelegate/* 状態情報保存の有効化 */- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{ return YES;}/* 状態情報復元の有効化 */- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{ return YES;}@end

2013年 9月 8日 日曜日

Page 10: iOS 状態の保存と復元

アプリケーション固有の状態

@implementation AppDelegate/* 版その他、アプリケーションの他の状態情報をエンコード */- (void)application:(UIApplication *)application willEncodeRestorableStateWithCoder:(NSCoder *)coder{}

/* 版その他、アプリケーションの他の状態情報をデコード */- (void)application:(UIApplication *)application didDecodeRestorableStateWithCoder:(NSCoder *)coder{ [[UIApplication sharedApplication] extendStateRestoration]; dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(globalQueue, ^{ dispatch_async(dispatch_get_main_queue(), ^{ [[UIApplication sharedApplication] completeStateRestoration]; }); }); NSString *restoreBundleVersion = [coder decodeObjectForKey:UIApplicationStateRestorationBundleVersionKey]; DBGMSG(@"Restore bundle version = %@", restoreBundleVersion); NSNumber *restoreUserInterfaceIdiom = [coder decodeObjectForKey:UIApplicationStateRestorationUserInterfaceIdiomKey]; DBGMSG(@"Restore User Interface Idiom = %d", restoreUserInterfaceIdiom.integerValue);}@end

2013年 9月 8日 日曜日

Page 11: iOS 状態の保存と復元

独自のビューコントローラの生成

@implementation AppDelegate+ (UIViewController *)viewControllerWithRestorationIdentifierPath: (NSArray *)identifierComponents coder:(NSCoder *)coder{ DBGMSG(@"%s", __func__); for (NSString *identifier in identifierComponents) { DBGMSG(@"identifier:%@", identifier); } return nil;}@end

2013年 9月 8日 日曜日

Page 12: iOS 状態の保存と復元

@implementation DetailViewController

- (void)encodeRestorableStateWithCoder:(NSCoder *)coder{ DBGMSG(@"DetailViewController: encodeRestorableStateWithCoder"); [super encodeRestorableStateWithCoder:coder]; [[Document sharedDocument] save]; [coder encodeInteger:self.detailItemIndex forKey:kUnsavedDetailItemIndexKey];}

- (void)decodeRestorableStateWithCoder:(NSCoder *)coder{ DBGMSG(@"DetailViewController: decodeRestorableStateWithCoder"); [super decodeRestorableStateWithCoder:coder]; self.detailItemIndex = [coder decodeIntegerForKey:kUnsavedDetailItemIndexKey]; DBGMSG(@"detailItemIndex:%d", self.detailItemIndex); [self configureView];}

+ (UIViewController *) viewControllerWithRestorationIdentifierPath:(NSArray *)identifierComponents coder:(NSCoder *)coder{ DBGMSG(@"%s", __func__); for (NSString *identifier in identifierComponents) { DBGMSG(@"identifier:%@", identifier); } return nil;}

@end

個々のビューコントローラでも

2013年 9月 8日 日曜日

Page 13: iOS 状態の保存と復元

サンプルコード

モデルテーブル 詳細表示

モデルはアプリで保存インデックスを保存と復元

2013年 9月 8日 日曜日

Page 14: iOS 状態の保存と復元

Demo

2013年 9月 8日 日曜日