如何在iPhone首次使用时确定负载应用程序?

3

我希望第一次加载我的应用程序时,它能够从我设置的首选项视图开始,而每次以后则从主视图开始。

我找不到一种方法来检测是否是第一次运行应用程序。有什么建议吗?

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

    // Override point for customization after application launch
    NSUserDefaults      *padFactoids;
    int                 launchCount;

    padFactoids = [NSUserDefaults standardUserDefaults];
    launchCount = [padFactoids integerForKey:@"launchCount" ] + 1;
    [padFactoids synchronize];

    NSLog(@"this is the number: %i of times this app has been launched", launchCount);
        if ( launchCount == 1 )
    {
        NSLog(@"this is the FIRST LAUNCH of the app");
        // do stuff here as you wish
        bbb = [[Blue alloc]init];
        [window addSubview:bbb.view];
    }
    if ( launchCount >= 2 )
    {
        NSLog(@"this is the SECOND launch of the damn app");
        // do stuff here as you wish
        rrr = [[Red alloc]init];
        [window addSubview:rrr.view];
    }
    [window makeKeyAndVisible];

    return YES;
}

这里红色和蓝色是uiviewcontroller的子类,两个类中唯一的区别是在-(void)viewDidLoad{ self.view.backgroundcolor = [UIColor redColor]; }中,红色类显示红色背景颜色,而蓝色类显示蓝色背景颜色。但是当我执行应用程序时,它只显示蓝色,不显示红色。我错在哪里?第二次运行应用程序时,我该怎么做才能显示红色......

4个回答

18

以下是具体操作步骤。您会发现它非常简单,只需要 FOUR 行代码。

将此代码添加到您想要的任何位置。也许只需在您的 AppDelegate.m 文件中的 application:didFinishLaunchingWithOptions: 函数中添加即可。或者,无论您为应用程序进行一般设置的地方都可以。(但是,请确保它只能运行一次。)

NSUserDefaults      *padFactoids;
int                 launchCount;

padFactoids = [NSUserDefaults standardUserDefaults];
launchCount = [padFactoids integerForKey:@"launchCount" ] + 1;
[padFactoids setInteger:launchCount forKey:@"launchCount"];
[padFactoids synchronize];

NSLog(@"number of times: %i this app has been launched", launchCount);

if ( launchCount == 1 )
    {
    NSLog(@"this is the FIRST LAUNCH of the app");
    // do stuff here as you wish
    }
if ( launchCount == 2 )
    {
    NSLog(@"this is the SECOND launch of the damn app");
    // do stuff here as you wish
    }

// enjoy!

除了最简单的应用程序,几乎每个应用程序都会这样做。希望它能有所帮助。理论上,您并不一定需要费心进行“同步”调用,但我们发现在大量实际用户运行中,如果包括它可能更可靠。

PS 不要在首选项中使用布尔值。 如果你是一名新程序员,很难理解默认值,因此永远无法维护。坚持使用整数。(当第一次未使用时,它们总是“整数零”,因此没有问题。) 很容易。希望它能有所帮助。


1

从NSUserdefaults检查一些标志,如果没有,则显示首选项视图,并在这些设置被设置后设置该标志。

BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
    NSLog(@"Show panel");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
}

请详细解释。 - iOS_User

0
BOOL hasBeenStarted = [[NSUserDefaults standardUserDefaults] boolForKey:@"hasBeenStarted"];
if (!hasBeenStarted) {
    NSLog(@"Show panel");
    [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"hasBeenStarted"];
    [[NSUserDefaults standardUserDefaults]synchronize];// save to NSUserDefaults

}

0
最好在应用程序启动时创建一个本地文件,并在每次启动时检查文件是否存在。
对于NSUserDefault,如果应用程序没有在后台运行,则会重新初始化。
- (void)checkApplicationFirstLaunch
{
    if(![self getFileExistence:@"appinstall"])
    {
        // this is fresh install of the app
        [self createFileFromString:@"install" filename:@"appinstall"];
    }
    else
    {
        // app was already installed
    }
}

这里是我之前使用的方法

- (BOOL) getFileExistence: (NSString *) filename
{
    BOOL IsFileExists = NO;

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *favsFilePath = [documentsDir stringByAppendingPathComponent:filename];

    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    if ([fileManager fileExistsAtPath:favsFilePath])
    {
        IsFileExists = YES;
    }
    return IsFileExists;
}

- (void)createFileFromString:(NSString *)string filename:(NSString *)filename
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents directory
    NSLog(@"%@", documentsDirectory);
    NSError *error;
    BOOL succeed = [string writeToFile:[documentsDirectory stringByAppendingPathComponent:filename] atomically:YES encoding:NSUTF8StringEncoding error:&error];
    if (!succeed){
        // Handle error here
        NSLog(@"Did not save: Error: %@", error);
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接