1、创建window分类
2、沙盒进行版本判断
3、新特性控制器切换
创建window分类,判断版本
#import "UIWindow+ALExtension.h"
#import "ALTabBarViewController.h"
#import "ALNewViewController.h"
@implementation UIWindow (ALExtension)
- (void)switchRootViewController {
// 之前已经登录成功过
NSString *key = @"CFBundleVersion";
// 上一次的使用版本(存储在沙盒中的版本号)
NSString *lastVersion = [[NSUserDefaults standardUserDefaults] objectForKey:key];
// 当前软件的版本号(info.plist中获得)
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[key];
if ([currentVersion isEqualToString:lastVersion]) {
// 版本相同,这次打开和上次打开是同一个版本
self.rootViewController = [[ALTabBarViewController alloc] init];
} else {
// 这次打开的和上一次不一样
self.rootViewController = [[ALNewViewController alloc] init];
// 将当前的版本号存进沙盒
[[NSUserDefaults standardUserDefaults]setObject:currentVersion forKey:key];
// 马上同步到沙盒
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
新特性控制器
#import "ALNewViewController.h"
#import "ALTabBarViewController.h"
#define KWidth self.view.frame.size.width
#define KHeight self.view.frame.size.height
#define ALNewCount 4
@interface ALNewViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@end
@implementation ALNewViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 创建图片
for (int i = 0; i < ALNewCount; i++) {
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(i * KWidth, 0, KWidth, KHeight)];
imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"workth0%d", i + 1]];
[self.scrollView addSubview:imageView];
if (i == ALNewCount - 1) {
[self setupLastImageView:imageView];
}
}
self.pageControl.numberOfPages = ALNewCount;
self.scrollView.contentSize = CGSizeMake(ALNewCount * KWidth, 0);
}
#pragma mark - 进入主界面
- (void)setupLastImageView:(UIImageView *)imageView {
// 开启用户交互
imageView.userInteractionEnabled = YES;
UIButton *enter = [[UIButton alloc] init];
enter.frame = CGRectMake(0, KHeight - 100, KWidth, 30);
[enter setTitle:@"进入阿里星球之旅" forState:UIControlStateNormal];
[enter setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forState:UIControlStateNormal];
[imageView addSubview:enter];
[enter addTarget:self action:@selector(startClick) forControlEvents:UIControlEventTouchUpInside];
}
// 切换
- (void)startClick {
UIWindow *window = [UIApplication sharedApplication].keyWindow;
window.rootViewController = [[ALTabBarViewController alloc] init];
}
#pragma UIScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
self.pageControl.currentPage = (int)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
}