アプリケーションが起動されたかどうかを覚えておく必要がある場合は、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のユーザーのようなホームボタンがあります。
ファイルに書き込む必要がありますか、またはファイルを閉じてもブール値を保持できますか? –
起動から起動まで何らかの方法で保存しなければなりません(バックグラウンドで再開しても、完全に閉じたときには確実に再開できません)。ファイルに保存することが私にとって最良の方法のようです。 – Nitrex88