iOS 开发 判断应用程序是否为 第一次/首次 打开【附代码】

今天爱分享给大家带来iOS 开发 判断应用程序是否为 第一次/首次 打开【附代码】,希望能够帮助到大家。

第一步:在AppDelegate中当应用启动完成后加入一下代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //NSUserDefaults非常好用,并不需要用户在程序中设置NSUserDefaults的全局变量,需要在哪里使用NSUserDefaults的数据,那么就在哪里创建一个NSUserDefaults对象,然后进行读或者写澡作。
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
    }else{
         [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
    }
    // Override point for customization after application launch.
    return YES;
}

第二步:在ViewController的.m的ViewDidLoad方法中添加如下代码:

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"firstLaunch"]) {
        // 这里判断是否第一次
        UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"第一次"
                                                      message:@"进入App"
                                                     delegate:self
                                            cancelButtonTitle:@"我知道了"
                                            otherButtonTitles:nil];
        [alert show];//使用UIAlertView显示应用是否为第一次启动
    }
}
原文链接:https://blog.itblood.com/979.html,转载请注明出处。
0
我和朋友陪妈妈去露营 (Camp With Mom Extend)官方中文版 [PC+安卓] [1.5G] [SLG/中文/动态]
我和朋友陪妈妈去露营 (Camp With Mom Extend)官方中文版 [PC+安卓] [1.5G] [SLG/中文/动态]
6分钟前 有人购买 去瞅瞅看

站点公告

显示验证码
没有账号?注册  忘记密码?