您的位置:首页 > 其它

应用程序是否首次运行

2016-06-08 14:41 211 查看
.h文件中定义方法

#import "AppDelegate.h"

@interface AppDelegate (Helper)

//  检测当前版本的应用程序是否首次运行

+ (BOOL)isFirstLaunching;

#import "AppDelegate+Helper.h"

@implementation AppDelegate (Helper)

.m文件中实现方法

/*

 是否为版本升级后的第一次打开程序

 */

+ (BOOL)isFirstLaunching

{

    //从plist文件中取出版本号

    NSString *currentVersion = [[[NSBundle
mainBundle] infoDictionary]
objectForKey:@"CFBundleShortVersionString"];

    

    //取出NSUserDefaults中的版本号

    NSUserDefaults *defaults = [NSUserDefaults
standardUserDefaults];

    NSString *lastRunVersion = [defaults
objectForKey:LAST_RUN_VERSION_KEY];

    CYBLog(@">>>>>pre——Login11-1---lastRunVersion=%@",lastRunVersion);

   
//应用程序全新安装,返回yes进入引导页面

    if (!lastRunVersion) {

        [defaults setObject:currentVersion
forKey:LAST_RUN_VERSION_KEY];

        [defaults setObject:[NSNumber
numberWithInt:1]
forKey:kAppLaunchTimesKey];

        return YES;

    }

    CYBLog(@">>>>>pre——Login11-2---currentVersion=%@",currentVersion);

   
//应用程序升级安装

    if (![lastRunVersion
isEqualToString:currentVersion]) {

        [defaults setObject:currentVersion
forKey:LAST_RUN_VERSION_KEY];

       
//重置应用程序启动次数计数器以及打分提醒设置

        [defaults setObject:[NSNumber
numberWithInt:1]
forKey:kAppLaunchTimesKey];

        [defaults synchronize];

        

        return YES;

    }

    // 
应用程序启动次数+1

    int appLaunchTimes = [[defaults
objectForKey:kAppLaunchTimesKey]
intValue];

    [defaults setObject:[NSNumber
numberWithInt:++appLaunchTimes]
forKey:kAppLaunchTimesKey];

    [defaults synchronize];

    CYBLog(@">>>>>pre——Login11-3---启动次数%d",appLaunchTimes);

    

    return
NO;

}

AppDelegate.m中使用

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

 if ([AppDelegate
isFirstLaunching]) {

//引导页

        AppGuideViewController *appGuideVc = [[AppGuideViewController
alloc]
initWithNibName:@"AppGuideViewController"
bundle:nil];

        UINavigationController * navigationVc = [[UINavigationController
alloc] initWithRootViewController:appGuideVc];

        self.window.rootViewController = navigationVc;

        return YES;

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: