iOS程序更新后新特性介绍界面的实现

来源:互联网 发布:mac新建的文件夹在哪里 编辑:程序博客网 时间:2024/04/29 23:42

今天一起更新了好多软件,突然注意到每个软件更新版本之后都会先出现一个新版本功能引导提示界面,然后在跳转到正常的实现界面,那么如何做才能实现上述功能呢。

思路比较简单,最重要的一点就是如何判断用户是否第一次使用软件当前的版本,如果第一次使用这个版本,则显示版本新功能界面,否则直接显示主界面。APP有一个特点就是软件如果没有被卸载过,那么在沙盒里的东西是不会变的,我们刚好可以利用这一点,通过判断沙盒中有无软件当前的版本号作为判定是否是第一次使用该版本软件的标准,如果沙盒中没有当前版本号,则是第一次使用,显示版本新功能提示界面,并将该版本号写入沙盒中;如果沙盒中有该版本号,则判定不是第一次使用该版本,直接进入主界面。

由于这部分工作实在加载主界面之前完成的,因此应该在AppDelegate里面的application:didFinishLaunchingWithOptions这个方法里面完成上述功能;

首先新建了两个控制器分别用来模拟新特性引导界面与程序的正常应用主界面


#import "MainViewController.h"@interface MainViewController()@end@implementation MainViewController-(void)viewDidLoad{    [super viewDidLoad];    //背景颜色设置为白色    self.view.backgroundColor = [UIColor whiteColor];        //创建一个button来显示主界面    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(150, 250, 200, 50)];    [button setTitle:@"主界面" forState:UIControlStateNormal];    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [self.view addSubview:button];}@end

#import "NewFeatureViewController.h"@interface NewFeatureViewController ()@end@implementation NewFeatureViewController-(void)viewDidLoad{    [super viewDidLoad];    //背景颜色为白色    self.view.backgroundColor = [UIColor whiteColor];        //用一button来显示新特性界面    UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 250, 200, 50)];    [button setTitle:@"新特性导航界面" forState:UIControlStateNormal];    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];    [self.view addSubview:button];}@end

然后在AppDelegate.m中修改application:didFinishLaunchingWithOptions方法

#import "AppDelegate.h"#import "MainViewController.h"#import "NewFeatureViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {        self.window = [[UIWindow alloc]initWithFrame:SCREEN_BOUNDS];       //通过kCFBundleVersionKey获取程序版本号信息,此处要强制转换成Nsstring形式    NSString *versionKey = (NSString*)kCFBundleVersionKey;    //去沙盒中取出上次使用过的版本号    NSString *lastVersionCode = [[NSUserDefaults standardUserDefaults] objectForKey:versionKey];    //加载程序中的info.plist文件获得当前程序的版本号    NSString *nowValueCode = [NSBundle mainBundle].infoDictionary[versionKey];            //比较两个版本号是否相同    if([lastVersionCode isEqualToString:nowValueCode]){        //相等的话表示不是第一次打开此版本,直接加载微博主界面        MainViewController *main = [[MainViewController alloc]init];        self.window.rootViewController = main;    } else {        //第一次使用软件,先把版本号写入沙盒        [[NSUserDefaults standardUserDefaults] setObject:nowValueCode forKey:versionKey];        //调用synchronize强制存储,如果不加则随机存储        [[NSUserDefaults standardUserDefaults] synchronize];                //显示新特性导航界面        NewFeatureViewController *new = [[NewFeatureViewController alloc]init];        self.window.rootViewController = new;    }        //显示窗口    [self.window makeKeyAndVisible];    return YES;}@end

测试效果如图:

程序第一次运行的时候显示新特性导航界面



第二次运行则显示程序主界面


如果在plist文件中修改了版本号,在运行的话就显示新特性导航界面


0 0
原创粉丝点击