开发者

Create Action View to Only Be Displayed First Time App is Ran

I am just trying to have an action view display the very first time the app is opened, but I am not sure how to build this.

I understand it must be put 开发者_运维问答in

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

I have the action view created, but do not know how to have it display only on the first time, and never again.


Your application if going to have to remember if it's been launched or not before so you can either show the actionview or not. One way you can do this is to save a boolean to file on application exits and read it back at application launch (and if it exists, the apps been launched before). Here is some code that does something like this (put it in your application delegate).

- (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];
}

for saving and here is code for loading...

- (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;
}

Also note that if your running in the simulator this code will not execute if you quit the simulator, you actually have to press the home button like an iPhone user would.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜