2011-02-04 51 views
0

我只是試圖讓一個動作視圖顯示第一次打開應用程序,但我不知道如何構建這個。創建動作視圖僅顯示在第一次的應用程序是跑

我明白必須放在

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

我創建的操作觀點,但不知道如何把它僅在第一次顯示,永遠不再。

回答

0

如果您的應用程序需要記住它是否已啓動,那麼您可以顯示操作視圖或不顯示操作視圖。一種方法是在應用程序退出時將布爾值保存爲文件,並在應用程序啓動時將其讀回(如果存在,應用程序將在以前啓動)。這裏有一些代碼可以做這樣的事情(把它放在你的應用程序委託中)。

- (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用戶那樣的主頁按鈕會。

+0

它必須寫入一個文件,或者它可以保持一個布爾值,即使它關閉? – 2011-02-06 21:25:44

相關問題