ios - 打开appstore应用、打开appstore评论、打开其他应用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhyl8157121/article/details/51434411

废话少说,直接上代码:

  • 打开 appstore 应用界面:
NSString *appid = @"1234567";
NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/cn/app/id%@", appid];
NSURL *url = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:url];

其中,appid 是应用发布时,苹果声称的一串数字,不需要自己设置,和项目名称的 id 不一样。使用时,只需要把appid 改为自己的appid 即可,前面的url 不需要改。

appid 可以在 iTunes Connect 中获取到。

  • 打开 appstore 评论界面:

上面的方法能够打开 appstore 中某个应用界面,但是很多时候我们是希望用户直接上去评论的,所以,tab 页要直接打开评论那一页。

代码如下:

NSString *appid = @"1234567";
NSString *str = [NSString stringWithFormat:@"itms-apps://itunes.apple.com/app/viewContentsUserReviews?id=%@", appid];
NSURL *url = [NSURL URLWithString:str];
[[UIApplication sharedApplication] openURL:url];

可以看到,就是 url 有一些不同而已,大家根据需求选择。

  • 打开本地其他应用

某些情况下公司可能会有多款app,因此会有这样的需求:每个app中都有产品推荐功能,通过当前app能够打开其他app(已经安装的情况下),如果没有安装,则跳到 appStore下载。

比如说,输入法app 中可以推荐 搜狗搜索,当用户点击搜狗搜索图标时,检测当前用户手机上是否有该app。如果有,直接打开该 app,如果没有,则跳转到appStore 下载该app。

跳转到 appStore下载需要知道 该app 的url。从本地打开app 需要知道该 app 的id(项目名,比如 com.sogou.search) 以及协议名(可以有,可以没有,比如 sohu),最后构成的url 是 协议名://app的id ,比如 sohu://com.sogou.search

代码如下:

NSURL *customUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@",product.scheme,product.identifier ]];
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:customUrl])
{
        //有安装应用,打开应用
        [app openURL: customUrl];
}else{
        [app openURL:[NSURL URLWithString:product.url ]];
}

PS: 以上写法仅支持 iOS 7 及其以上版本,如果大家想要支持更低的版本,可以自行搜索。

猜你喜欢

转载自blog.csdn.net/zhyl8157121/article/details/51434411