若要在 App 內直接跳到該 App 在 App Store 的評論頁面,只要把下面網址中的 App_ID 換成自己的即可,其餘參數不必更動。
若想先用 Safari 驗證,把 itms-apps:// 換成 https:// 即可。也可以試著改動其中幾個參數(例如 pageNumber、sortOrdering),觀察結果差異。
注意:此功能必須以真機測試,模擬器無法正確處理 itms-apps 這種自訂 URL scheme。
NSString *urlString = @"itms-apps://ax.itunes.apple.com/WebObjects/"
@"MZStore.woa/wa/viewContentsUserReviews?mt=8"
"&onlyLatestVersion=true&pageNumber=0&sortOrdering=1"
"&type=Purple+Software&id=APP_ID";
NSURL *url = [NSURL URLWithString:urlString];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url];
} else {
NSLog(@"can not open");
}
mt=8 指定開啟的是 App Store(而非 iTunes Store 等其他媒體類型);id 是 App 的 Apple ID;onlyLatestVersion=true 表示只顯示最新版本的評論;pageNumber 與 sortOrdering 分別控制頁碼與排序方式。
要知道 App Store 上是否有新版本,做法是向 iTunes Lookup API 查詢自己的 App 資訊:
NSURL *lookup = [NSURL URLWithString:
@"https://itunes.apple.com/lookup?id=YOUR_APP_ID"];
NSURLSessionDataTask *task =
[[NSURLSession sharedSession] dataTaskWithURL:lookup
completionHandler:^(NSData *data, NSURLResponse *r, NSError *e) {
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data
options:0 error:nil];
NSArray *results = json[@"results"];
if (results.count > 0) {
NSString *storeVersion = results[0][@"version"];
NSString *localVersion =
[[NSBundle mainBundle].infoDictionary objectForKey:@"CFBundleShortVersionString"];
if ([storeVersion compare:localVersion
options:NSNumericSearch] == NSOrderedDescending) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showUpdateAlertWithVersion:storeVersion];
});
}
}
}];
[task resume];
這裡有兩個重點:一是版本比較必須用 NSNumericSearch,因為字串比較會把 "1.10" 判為小於 "1.9";二是 UI 更新必須回到主執行緒,NSURLSession 的回呼在背景佇列。
若目的只是「請使用者留評論」,那麼主動跳轉到評論頁其實不是蘋果建議的做法。正確方式是使用 StoreKit 的 SKStoreReviewController,它會以系統原生彈窗詢問,使用者可直接打分數而不必離開 App,轉換率也更高:
#import <StoreKit/StoreKit.h>
if (@available(iOS 10.3, *)) {
[SKStoreReviewController requestReview];
}
蘋果對它的呼叫頻率有限制(一年內同一版本不會重複提示),且不要在啟動時或強制更新時呼叫。至於「檢查新版本並提示更新」,則仍應自行呼叫 lookup API 判斷,再視情況引導使用者前往 App Store。