我只是想在第一次打开应用程序时显示一个动作视图,但我不确定如何构建它。
我知道它必须放入
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
我创建了动作视图,但不知道如何让它只在第一次显示,以后再也不显示。
我只是想在第一次打开应用程序时显示一个动作视图,但我不确定如何构建它。
我知道它必须放入
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
我创建了动作视图,但不知道如何让它只在第一次显示,以后再也不显示。
如果您的应用程序必须记住它之前是否已启动,那么您可以显示或不显示操作视图。您可以做到这一点的一种方法是在应用程序退出时将布尔值保存到文件中,并在应用程序启动时将其读回(如果存在,则应用程序之前已启动)。这是一些执行此类操作的代码(将其放入您的应用程序委托中)。
- (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 用户那样按下主页按钮。