0

アプリを初めて開いたときにアクションビューを表示しようとしていますが、これを構築する方法がわかりません。

入れる必要があることは理解しています

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

アクション ビューを作成しましたが、それを最初の 1 回だけ表示し、2 回目は表示しないようにする方法がわかりません。

4

1 に答える 1

0

アプリケーションが以前に起動されたかどうかを記憶する必要がある場合は、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 ユーザーのようにホーム ボタンを押す必要があります。

于 2011-02-04T23:01:58.460 に答える