ios 跳轉AppStore的兩種方法,應用內和直接跳轉
#import "ViewController.h"import <StoreKit/StoreKit.h>
@interface ViewController ()<SKStoreProductViewControllerDelegate> @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //第一種方法 直接跳轉 UIButton btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 50)]; btn.backgroundColor = [UIColor redColor]; [btn setTitle:@"直接跳轉" forState:UIControlStateNormal]; btn.tag = 1; [btn addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside]; //第二中方法 應用內跳轉 UIButton btnT = [[UIButton alloc]initWithFrame:CGRectMake(100, 300, 100, 50)]; btnT.backgroundColor = [UIColor purpleColor]; btnT.tag = 2; [btnT setTitle:@"應用內跳轉" forState:UIControlStateNormal]; [btnT addTarget:self action:@selector(btn:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; [self.view addSubview:btnT]; } - (void)btn:(UIButton )btn{ if (btn.tag == 1) { //第一種方法 直接跳轉 [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms-apps://itunes.apple.com/app/id1018221712"]]; }else{ //第二中方法 應用內跳轉 //1:導入StoreKit.framework,控制器里面添加框架#import <StoreKit/StoreKit.h> //2:實現代理SKStoreProductViewControllerDelegate SKStoreProductViewController storeProductViewContorller = [[SKStoreProductViewController alloc] init]; storeProductViewContorller.delegate = self; // ViewController viewc = [[ViewController alloc]init]; // __weak typeof(viewc) weakViewController = viewc; //加載一個新的視圖展示 [storeProductViewContorller loadProductWithParameters: //appId @{SKStoreProductParameterITunesItemIdentifier : @"1018221712"} completionBlock:^(BOOL result, NSError error) { //回調 if(error){ NSLog(@"錯誤%@",error); }else{ //AS應用界面 [self presentViewController:storeProductViewContorller animated:YES completion:nil]; } }]; } }
pragma mark - 評分取消按鈕監聽
//取消按鈕監聽 - (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController{ [self dismissViewControllerAnimated:YES completion:nil]; }</pre>