アプリケーションが以前に起動されたかどうかを記憶する必要がある場合は、actionview を表示するかどうかを指定できます。これを行う 1 つの方法は、アプリケーションの終了時にブール値をファイルに保存し、アプリケーションの起動時にそれを読み戻すことです (存在する場合、アプリは以前に起動されています)。このようなことを行うコードを次に示します (アプリケーション デリゲートに配置します)。
- (void)applicationWillResignActive:(UIApplication *)application {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saved_data.dat"];
NSMutableData *theData = [NSMutableData data];
NSKeyedArchiver *encoder = [[NSKeyedArchiver alloc] initForWritingWithMutableData:theData];
BOOL launched = YES;
[encoder encodeBool:launched forKey:@"launched"];
[encoder finishEncoding];
[theData writeToFile:path atomically:YES];
[encoder release];
}
保存用で、ロード用のコードは次のとおりです...
- (id) init {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"saved_data.dat"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if([fileManager fileExistsAtPath:path]) {
//open it and read it
NSLog(@"data file found. reading into memory");
NSData *theData = [NSData dataWithContentsOfFile:path];
NSKeyedUnarchiver *decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:theData];
BOOL launched = [decoder decodeBoolForKey:@"launched"];
if (launched) {
//APP HAS LAUNCHED BEFORE SO DON"T SHOW ACTIONVIEW
}
[decoder finishDecoding];
[decoder release];
} else {
NSLog(@"no data file found.");
//APP HAS NEVER LAUNCHED BEFORE...SHOW ACTIONVIEW
}
return self;
}
また、シミュレーターで実行している場合、シミュレーターを終了するとこのコードが実行されない場合は、実際には iPhone ユーザーのようにホーム ボタンを押す必要があります。