ios - 打開appstore應用、打開appstore評論、打開其他應用

廢話少說,直接上代碼:

  • 打開 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 及其以上版本,如果大家想要支持更低的版本,可以自行搜索。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章