一测蹲、第 1 種:直接跳轉(zhuǎn) demo
直接跳轉(zhuǎn)效果
-
1.1菇怀、Swift 版本
/// 跳轉(zhuǎn) @objc func click1() { let url = URL(string: "itms-apps://itunes.apple.com/app/id 1142110895") // 注意: 跳轉(zhuǎn)之前, 可以使用 canOpenURL: 判斷是否可以跳轉(zhuǎn) if !UIApplication.shared.canOpenURL(url!) { // 不能跳轉(zhuǎn)就不要往下執(zhí)行了 return } if #available(iOS 10.0, *) { UIApplication.shared.open(url!, options: [:]) { (success) in if (success) { print("10以后可以跳轉(zhuǎn)url") }else{ print("10以后不能完成跳轉(zhuǎn)") } } } else { // Fallback on earlier versions let success = UIApplication.shared.openURL(url!) if (success) { print("10以下可以跳轉(zhuǎn)") }else{ print("10以下不能完成跳轉(zhuǎn)") } } }
提示:上面 id429849944 中 id 后面的數(shù)字是app的唯一id袁串,我們可以在app store 獲取任意一個(gè)app的id
獲取APP的唯一id- 如果app未上線我們可以登錄apple開發(fā)者賬號(hào)传惠,點(diǎn)開自己創(chuàng)建的應(yīng)用獲取app唯一的id
-
1.2记盒、OC版本
- (void)click1 { NSURL *url = [NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1142110895"]; if (@available(iOS 10.0, *)){ [[UIApplication sharedApplication]openURL:url options:@{UIApplicationOpenURLOptionsSourceApplicationKey:@YES} completionHandler:^(BOOL success) { if (success) { NSLog(@"10以后可以跳轉(zhuǎn)url"); }else{ NSLog(@"10以后不可以跳轉(zhuǎn)url"); } }]; }else{ BOOL success = [[UIApplication sharedApplication]openURL:url]; if (success) { NSLog(@"10以前可以跳轉(zhuǎn)url"); }else{ NSLog(@"10以前不可以跳轉(zhuǎn)url"); } } }
二、第 2 種: 應(yīng)用內(nèi)跳轉(zhuǎn) demo
應(yīng)用內(nèi)跳轉(zhuǎn)效果
-
2.1慷丽、Swift 版本
導(dǎo)入頭文件
import StoreKit
-
添加跳轉(zhuǎn)
@objc func click2() { let storeProductVC = StoreKit.SKStoreProductViewController() storeProductVC.delegate = self let dict = [SKStoreProductParameterITunesItemIdentifier: "1142110895"] storeProductVC.loadProduct(withParameters: dict) { (result, error) in guard error == nil else { return } } present(storeProductVC, animated: true, completion: nil) }
-
實(shí)現(xiàn)協(xié)議回調(diào)
extension ViewController: SKStoreProductViewControllerDelegate { func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) { viewController.dismiss(animated: true, completion: nil) } }
-
2.2、OC版本
導(dǎo)入頭文件
#import <StoreKit/StoreKit.h>
鳄哭,遵守協(xié)議:<SKStoreProductViewControllerDelegate>
-
添加跳轉(zhuǎn)
//2:實(shí)現(xiàn)代理SKStoreProductViewControllerDelegate SKStoreProductViewController *storeProductViewContorller = [[SKStoreProductViewController alloc] init]; storeProductViewContorller.delegate = self; //加載一個(gè)新的視圖展示 [storeProductViewContorller loadProductWithParameters: @{SKStoreProductParameterITunesItemIdentifier : @"1142110895"} completionBlock:^(BOOL result, NSError *error) { //回調(diào) if(error){ NSLog(@"錯(cuò)誤%@",error); }else{ //應(yīng)用界面 [self presentViewController:storeProductViewContorller animated:YES completion:nil]; } }];
-
實(shí)現(xiàn)協(xié)議(取消按鈕監(jiān)聽)回調(diào)
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{ [self dismissViewControllerAnimated:YES completion:nil]; }