「歡迎回來」與「我們升級了,看看新功能」是兩種很常見的引導文案。要判斷這兩件事,用 NSUserDefaults 記兩個旗標就夠了。
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"didFinishLaunchingWithOptions");
// 是否為首次啟動
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"isFirstLaunched"]) {
NSLog(@"AppDelegate - didFinishLaunchingWithOptions: App was already launched.");
} else {
NSLog(@"AppDelegate - didFinishLaunchingWithOptions: App's first launch.");
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isFirstLaunched"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
// 版本是否改變
if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"clientVersion"]
isEqualToString:CLIENT_VERSION]) {
NSLog(@"AppDelegate - didFinishLaunchingWithOptions: App version remains the same.");
} else {
NSLog(@"AppDelegate - didFinishLaunchingWithOptions: App version did update.");
[[NSUserDefaults standardUserDefaults] setObject:CLIENT_VERSION forKey:@"clientVersion"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
return YES;
}
直接從 bundle 的 Info.plist 讀出「顯示版本」比較直覺:
#define CLIENT_VERSION [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]
另外,若只是想在 App Store 有新版本時提示使用者,別忘了還有另一條路:呼叫 iTunes lookup API 比對線上版號,這比在本地猜測更可靠。