验证App是否第一次启动
当我们安装一个App的时候 ,经常会有这样的情况 ,第一次启动的时候,会有一个启动的类似轮播图的界面,等到再次启动的时候就没有了
具体实现:
1.新建两个视图控制器,
一个名为 TabBarController ,另一个名为 FirstInputViewController(第一次启动时候要显示的视图控制器)
2.在FirstInputViewController里写入代码
1 - (void)viewDidLoad { 2 [super viewDidLoad]; 3 self.view.backgroundColor =[UIColor blueColor]; 4 //将当前版本号保存在nowVersion里 5 NSString *nowVersion =[[[NSBundle mainBundle]infoDictionary]objectForKey:(NSString *)kCFBundleVersionKey]; 6 //为NSUserDefaults添加新的键名和键值 7 NSUserDefaults *defau =[NSUserDefaults standardUserDefaults]; 8 [defau setObject:nowVersion forKey:@"App_Version"]; 9 10 // Do any additional setup after loading the view. 11 }
3.在Appdelegate里设置启动的根视图
1 #import "AppDelegate.h" 2 #import "TabBarController.h" 3 #import "FirstInputViewController.h" 4 @interface AppDelegate () 5 @end 6 @implementation AppDelegate 7 - (BOOL)application:(UIApplication *)application 8 didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 9 //获取当前程序的版本号 10 NSString *nowVesion =[[[NSBundle mainBundle]infoDictionary] objectForKey:(NSString *)kCFBundleVersionKey]; 11 //获取NSUserDefaults里的键值 12 NSUserDefaults *defau =[NSUserDefaults standardUserDefaults]; 13 NSString *vesion=[defau objectForKey:@"App_Version"]; 14 //进行比较来选择根视图控制器 15 if (![nowVesion isEqualToString:vesion]) { 16 self.window.rootViewController=[[FirstInputViewController alloc]init]; 17 }else{ 18 self.window.rootViewController=[[TabBarController alloc]init]; 19 } 20 [self.window makeKeyAndVisible]; 21 // Override point for customization after application launch. 22 return YES; 23 }
什么是NSUserDefaults?
在以上的代码中,我们用到了一个类,叫做NSUserDefaults
NSUserDefaults是一个单例,在整个程序中只有一个实例对象,他可以用于数据的永久保存,本质像是一个PLIST文件
要注意的一点是:NSUserDefaults对相同的Key赋值约等于一次覆盖,要保证每一个Key的唯一性
NSUserDefaults支持的数据类型有:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL
详细用法: http://my.oschina.net/u/1245365/blog/294449
转载请注明出处:今思采薇无 http://www.cnblogs.com/pangxuhui/

浙公网安备 33010602011771号