版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Maple_ROSI/article/details/52849533
在项目中我们经常会看到下载的应用第一进入或者更新之后再进来时会看到对本应用的一些介绍及使用,这就是新手引导,下面我们简单的实现新手引导页面;
1.创建一个继承UIWindow的类目命名为 UIWindow+Extension
,然后在这个类目做我们的实现操作
#import "BSTabBarController.h"
#import "BSNewfeatureViewController.h"
#import "AppDelegate.h"
#define bundleVersionKey @"CFBundleVersion"
@implementation UIWindow (Extension)
- (void)switchRootViewController
{
// 上一次的使用版本(存储在沙盒中的版本号)
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:bundleVersionKey];
// 当前软件的版本号(从Info.plist中获得)
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[bundleVersionKey];
if ([currentVersion isEqualToString:lastVersion]) { // 版本号相同:这次打开和上次打开的是同一个版本
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.tabBar = [[BSTabBarController alloc] init];
self.rootViewController = app.tabBar;
} else { // 这次打开的版本和上一次不一样,显示新特性
self.rootViewController = [[BSNewfeatureViewController alloc] init];
}
}
// 当前的版本号存进沙盒
- (void)saveCurrentVersion
{
// 将当前的版本号存进沙盒
[[NSUserDefaults standardUserDefaults] setObject:[NSBundle mainBundle].infoDictionary[bundleVersionKey] forKey:bundleVersionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}
@end
2.在AppDelegate.h文件中,BSTabBarController是自己封装继承UITabBarController的类
@class BSTabBarController;
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) BSTabBarController *tabBar;
@end
3.在AppDelegate.m文件中,在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
中调用如下方法
[self.window switchRootViewController];
4."BSNewfeatureViewController.h"
中代码实现需要的效果,这里就不具体实现了
5.当用户浏览完引导界面或点击按钮进入主界面时,切换应用的主控制器
- (void)startClick
{
// 切换到TabBarController
UIWindow *window = [UIApplication sharedApplication].keyWindow;
[window saveCurrentVersion];
AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];
app.tabBar = [[BSTabBarController alloc] init];
window.rootViewController = app.tabBar;
}