前言
目前很多應用是要求點擊事件直接跳轉到App Store,最近工作中就遇到了一個跳轉 App Store 評分或者下載更新的功能,網上查到很多跳轉方法,這里記錄一下,下面話不多說了,來一起看看詳細的介紹吧。
主要跳轉方法有兩種
使用官方框架
蘋果提供了StoreKit.framework框架,工程中可以導入這個框架的主頭文件
#import <StoreKit/StoreKit.h>,
也可以直接導入
#import<StoreKit/SKStoreProductViewController.h >,
添加代理并實現代理方法<SKStoreProductViewControllerDelegate>
示例代碼
/** 應用內跳轉到App Store頁 */- (IBAction)jump:(id)sender { NSString *appId = @"1066602104"; // 創建對象 SKStoreProductViewController *storeVC = [[SKStoreProductViewController alloc] init]; // 設置代理 storeVC.delegate = self; // 初始化參數 NSDictionary *dict = [NSDictionary dictionaryWithObject:appId forKey:SKStoreProductParameterITunesItemIdentifier]; // 跳轉App Store頁 [storeVC loadProductWithParameters:dict completionBlock:^(BOOL result, NSError * _Nullable error) { if (error) { NSLog(@"錯誤信息:%@",error.userInfo); } else { // 彈出模態視圖 [self presentViewController:storeVC animated:YES completion:nil]; } }]; }#pragma mark -- SKStoreProductViewControllerDelegate/** SKStoreProductViewControllerDelegate 方法,選擇完成之后的處理 @param viewController SKStoreProductViewController */- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{ NSLog(@"將要退出 App Store 頁面了"); [viewController dismissViewControllerAnimated:YES completion:^{ NSLog(@"已經退出 App Store 頁面完成了"); }];}
應用內跳轉直接到 App Store 頁面
此方法使用 [[UIApplication sharedApplication] openURL:url];
打開鏈接的方式跳轉到App Store。
此方法主要是需要拿到自己要跳轉 App 的 App Store 的 URL 地址
/** 直接跳轉 */- (IBAction)justJumpToOtherPage:(id)sender { // 應用地址 NSString *appStr = @"https://itunes.apple.com/app/apple-store/id1317248738?pt=118536605&ct=web&mt=8"; // 跳轉 NSDictionary *options = @{UIApplicationOpenURLOptionUniversalLinksOnly :@(YES)}; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStr] options:options completionHandler:^(BOOL success) { NSLog(@"將要進入 App Store 頁面了"); }]; }- (IBAction)jumpToScorePage{ NSLog(@"將要進入 App Store 評分頁面了"); // 評分頁面地址 NSString *scoreStr = [NSString stringWithFormat:@"https://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=%@&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8",appID]; // 判斷系統用對應方法 if ( @available(iOS 10 , * )) { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:scoreStr] options:options completionHandler:^(BOOL success) { NSLog(@"已經進入 App Store 頁面了"); }]; } else { [[UIApplication sharedApplication] openURL:[NSURL URLWithString:appStr]]; }}
【注意】 跳轉 App Store 需要真機運行
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答