展示新版本特性的常見做法是:在使用者第一次開啟某個新版本時,顯示該版本的新功能介紹。判斷與實作要點如下:
1. 何時顯示版本新特性:並非「第一次使用某軟體」時顯示,而是「第一次使用某個版本」時顯示。
2. 如何取得版本號:在 plist 檔案的 bundle version 中可看到版本號。
3. 如何顯示:應在 YYAppDelegate.m 中判斷——若是第一次使用此版本,就將版本新特性控制器設為 window 的根控制器;否則顯示「首頁」控制器。新特性畫面可用 UIScrollView 搭配 UIPageControl 實作。
4. 如何知道是否第一次使用:把每次使用的版本號存到沙盒,下次開啟時取出上次保存的版本號比對即可。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// 1. 建立視窗
self.window = [[UIWindow alloc] init];
self.window.frame = [UIScreen mainScreen].bounds;
// 2. 設定根控制器
NSString *versionKey = @"CFBundleVersion";
versionKey = (__bridge NSString *)kCFBundleVersionKey;
// 取出上次儲存的版本號
NSUserDefaults *defaults = [[NSUserDefaults alloc] init];
NSString *lastVersion = [defaults objectForKey:versionKey];
// 取得當前版本號
NSString *currentVersion = [NSBundle mainBundle].infoDictionary[versionKey];
if ([currentVersion isEqualToString:lastVersion]) {
// 版本相同,顯示首頁
self.window.rootViewController = [[YYTabBarViewController alloc] init];
} else {
// 版本不同,顯示新版本特性
self.window.rootViewController = [[YYNewfeatureViewController alloc] init];
[defaults setObject:currentVersion forKey:versionKey];
[defaults synchronize];
}
// 3. 顯示主視窗
[self.window makeKeyAndVisible];
return YES;
}
程式碼要點:
(1)比較版本只需判斷字串是否相同,不必轉成浮點數。
(2)bundle version 在 Xcode 中的實際型別為 CFStringRef 相關型別。
(3)Foundation 與 Core Foundation 的資料型別可互相轉換,兩框架間較安全的做法是使用 __bridge 橋接轉換。
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupScrollView];
[self setupPageControl];
}
-(void)setupScrollView
{
UIScrollView *scrollView = [[UIScrollView alloc] init];
scrollView.frame = self.view.bounds;
scrollView.delegate = self;
[self.view addSubview:scrollView];
CGFloat imageW = scrollView.width;
CGFloat imageH = scrollView.height;
for (int i = 0; i < YYNewfeatureImageCount; i++) {
UIImageView *imageView = [[UIImageView alloc] init];
NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
if (FourInch) {
name = [name stringByAppendingString:@"-568h"];
}
imageView.image = [UIImage imageNamed:name];
[scrollView addSubview:imageView];
imageView.y = 0;
imageView.width = imageW;
imageView.height = imageH;
imageView.x = i * imageW;
}
scrollView.contentSize = CGSizeMake(YYNewfeatureImageCount * imageW, 0);
scrollView.backgroundColor = YYColor(246, 246, 246);
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.bounces = NO;
}
-(void)setupPageControl
{
UIPageControl *pageControl = [[UIPageControl alloc] init];
pageControl.numberOfPages = YYNewfeatureImageCount;
pageControl.centerX = self.view.width * 0.5;
pageControl.centerY = self.view.height - 30;
[self.view addSubview:pageControl];
pageControl.currentPageIndicatorTintColor = YYColor(253, 98, 42);
pageControl.pageIndicatorTintColor = YYColor(189, 189, 189);
self.pageControl = pageControl;
}
#pragma mark - UIScrollViewDelegate
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
double doublePage = scrollView.contentOffset.x / scrollView.width;
int intPage = (int)(doublePage + 0.5); // 四捨五入
self.pageControl.currentPage = intPage;
}
-(BOOL)prefersStatusBarHidden
{
return YES;
}
常用宏定義(可放在 pch 檔中):YYColor(r,g,b) 產生顏色、FourInch 判斷是否為 4 吋螢幕、YYLog 於 Debug 狀態才輸出日誌等。