iOS 状態の保存と復元
Click here to load reader
-
Upload
yukio-murakami -
Category
Technology
-
view
1.690 -
download
2
description
Transcript of iOS 状態の保存と復元
状態の保存と復元2013.9.7
村上幸雄 @m_yukio
2013年 9月 8日 日曜日
•村上幸雄•@m_yukio•ビッツ有限会社http://www.bitz.co.jp/
2013年 9月 8日 日曜日
本日の内容
• 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日 日曜日
サンプル
https://github.com/murakami/workbook/tree/master/ios/StateRestoration
2013年 9月 8日 日曜日
状態の保存と復元?• iOS 6から利用可能
• バックグラウンド実行時に停止させれ際に、停止前の状態を復帰させる
• 対象はビューコントローラとビュー。モデルはアプリで対応。
2013年 9月 8日 日曜日
ビューコントローラにIDをつけて、このIDで保存と復元をおこなう。
2013年 9月 8日 日曜日
ビューコントローラに独自の保存と復元のコードを追加する。
- (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日 日曜日
生成と破棄のタイミングに、状態の保存と復元のタイミングの考慮が加わる。
@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日 日曜日
状態の保存と復元は、アプリケーションのデリゲーションで有効/無効にできる。
@implementation AppDelegate/* 状態情報保存の有効化 */- (BOOL)application:(UIApplication *)application shouldSaveApplicationState:(NSCoder *)coder{ return YES;}/* 状態情報復元の有効化 */- (BOOL)application:(UIApplication *)application shouldRestoreApplicationState:(NSCoder *)coder{ return YES;}@end
2013年 9月 8日 日曜日
アプリケーション固有の状態
@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日 日曜日
独自のビューコントローラの生成
@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日 日曜日
@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日 日曜日
サンプルコード
モデルテーブル 詳細表示
モデルはアプリで保存インデックスを保存と復元
2013年 9月 8日 日曜日
Demo
2013年 9月 8日 日曜日