iOS 应用内打开 App Store 方式总结

方式一、通过SKStoreProductViewController (modal 样式)实现 在当前应用内打开 App Store

SKStoreProductViewController.gif
  1. 遵守 <SKStoreProductViewControllerDelegate>
  2. 实现 <SKStoreProductViewControllerDelegate>
#pragma mark - Download HLM in SKStoreProductViewController
- (void)openAppFromAppStore:(NSString *)appid {
    if (nil == appid || appid.length <= 0) {
        return;
    }
    
    //loading
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeClear];
    
//    // 改变导航栏的文字和图片颜色
//    [[UINavigationBar appearance] setTintColor:[UIColor greenColor]];
//    // 红色导航栏
//    [[UINavigationBar appearance] setBarTintColor:[UIColor redColor]];
//    [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[[SKStoreProductViewController class]]];
//    
    
    SKStoreProductViewController *store = [[SKStoreProductViewController alloc] init];
    store.delegate = self;
    NSDictionary<NSString *, id> *parameters = @{SKStoreProductParameterITunesItemIdentifier: appid};
    
    [store loadProductWithParameters:parameters completionBlock:^(BOOL result, NSError *error) {
        
        // finish loading
        [SVProgressHUD dismiss];
        
        if (error) {
            
            NSLog(@"error %@ with userInfo %@", error, [error userInfo]);
            
            // 提示用户发生了错误
//             或者通过 URL 打开 AppStore App.
            NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/in/app/id%@",HLMAPPID]];
            [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];
            
            
        } else {
            
            [self presentViewController:store animated:YES completion:NULL];
        }
    }];
}

/// 用户点击取消会执行该方法
- (void)productViewControllerDidFinish:(SKStoreProductViewController *)viewController {
    [viewController dismissViewControllerAnimated:YES completion:NULL];
}


  1. SKStoreProductViewController只能通过 modal 方式打开,否则会报错
//Terminating app due to uncaught exception 'SKUnsupportedPresentationException',
//reason: 'SKStoreProductViewController must be used in a modal view controller'

方式二、通过openURL:options:completionHandler:方式,使用 scheme 为 itms-apps:// (push 样式的) 启动App Store应用 打开下载界面

itms-apps.gif
    //push 样式的 App Store应用 打开下载界面 scheme > itms-apps://
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/id%@",HLMAPPID]];
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];

方式三、通过openURL:options:completionHandler:方式,使用 scheme 为 https://(push 样式的) 启动App Store应用 打开下载界面

https.gif
//push 样式的 App Store应用 打开下载界面 scheme > https://
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://itunes.apple.com/in/app/id%@",HLMAPPID]];
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:NULL];

测试用Demo

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容