IOS 應(yīng)用內(nèi)顯示 AppStore 某個(gè)應(yīng)用的詳情
前言
應(yīng)用內(nèi)跳轉(zhuǎn)到 AppStore 的文章很多,一般都是用 SKStoreProductViewController 來(lái)實(shí)現(xiàn)的,不知道有沒(méi)有在意一個(gè)問(wèn)題:打開(kāi)很慢!!怎么忍?!
正文
一般網(wǎng)上的文章的代碼:
func openAppStore(url: String){
if let number = url.rangeOfString("[0-9]{9}", options: NSStringCompareOptions.RegularExpressionSearch) {
let appId = url.substringWithRange(number)
let productView = SKStoreProductViewController()
productView.delegate = self
productView.loadProductWithParameters([SKStoreProductParameterITunesItemIdentifier : appId], completionBlock: { [weak self](result: Bool, error: NSError?) -> Void in
if result {
self?.presentViewController(productView, animated: true, completion: nil)
} else {
self?.openAppUrl(url)
}
})
} else {
openAppUrl(url)
}
}
private func openAppUrl(url: String) {
let nativeURL = url.stringByReplacingOccurrencesOfString("https:", withString: "itms-apps:")
if UIApplication.sharedApplication().canOpenURL(NSURL(string:nativeURL)!) {
UIApplication.sharedApplication().openURL(NSURL(string:url)!)
}
}
func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
實(shí)現(xiàn)的效果很好,就是很慢,點(diǎn)擊按鈕調(diào)用 openAppStore 要很久才能顯示出界面,就算加一個(gè)轉(zhuǎn)圈效果也很差。原因是因?yàn)橐?nbsp; linkmaker.itunes.apple.com 根據(jù) identifier 查找鏈接,仔細(xì)看代碼我們會(huì)發(fā)現(xiàn) presentViewController 是在查找到結(jié)果才被調(diào)用,其實(shí)我們可以不用讓界面現(xiàn)出來(lái),雖然時(shí)間是一樣的,但是用戶(hù)體驗(yàn)會(huì)很好,修改后代碼如下:
func openAppStore(url: String){
if let number = url.rangeOfString("[0-9]{9}", options: NSStringCompareOptions.RegularExpressionSearch) {
let appId = url.substringWithRange(number)
let productView = SKStoreProductViewController()
productView.delegate = self
productView.loadProductWithParameters([SKStoreProductParameterITunesItemIdentifier : appId], completionBlock: { [weak self](result: Bool, error: NSError?) -> Void in
if !result {
productView.dismissViewControllerAnimated(true, completion: nil)
self?.openAppUrl(url)
}
})
self.presentViewController(productView, animated: true, completion: nil)
} else {
openAppUrl(url)
}
}
private func openAppUrl(url: String) {
let nativeURL = url.stringByReplacingOccurrencesOfString("https:", withString: "itms-apps:")
if UIApplication.sharedApplication().canOpenURL(NSURL(string:nativeURL)!) {
UIApplication.sharedApplication().openURL(NSURL(string:url)!)
}
}
func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
viewController.dismissViewControllerAnimated(true, completion: nil)
}
代碼說(shuō)明:
不等 loadProductWithParameters 返回直接 presentViewController ,解析失敗再?lài)L試用 openURL 的方式打開(kāi)。
參考:
http://stackoverflow.com/questions/17871920/odd-behavior-with-skstoreproductviewcontroller
結(jié)束: 以上就是對(duì)ISO 應(yīng)用內(nèi)打開(kāi)AppStorn顯示某個(gè)應(yīng)用詳情,有需要的朋友可以參考下。
相關(guān)文章
swift3.0實(shí)現(xiàn)圖片放大縮小動(dòng)畫(huà)效果
這篇文章主要為大家詳細(xì)介紹了swift3.0實(shí)現(xiàn)圖片放大縮小動(dòng)畫(huà)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09
詳細(xì)整理iOS中UITableView的性能優(yōu)化
最近在微博上看到一個(gè)很好的開(kāi)源項(xiàng)目,是關(guān)于如何優(yōu)化UITableView的,加上正好最近也在優(yōu)化項(xiàng)目中的類(lèi)似朋友圈功能這塊,思考了很多關(guān)于UITableView的優(yōu)化技巧,所以決定詳細(xì)的整理下對(duì)優(yōu)化UITableView的理解,需要的朋友們可以參考借鑒。2017-03-03
iOS中 LGLAlertView 提示框的實(shí)例代碼
這篇文章主要介紹了iOS中 LGLAlertView 提示框的實(shí)例代碼非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-09-09
iOS App開(kāi)發(fā)中的UIStackView堆疊視圖使用教程
UIStackView是iOS9以來(lái)新增加的組件,使我們能夠?qū)IView子類(lèi)對(duì)象進(jìn)行靈活排版,這里我們就來(lái)看一下iOS App開(kāi)發(fā)中的UIStackView堆疊視圖使用教程2016-07-07
iOS UICollectionView刷新時(shí)閃屏的解決方法
本篇文章主要介紹了iOS UICollectionView刷新時(shí)閃屏的解決方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11
詳解iOS App開(kāi)發(fā)中Cookie的管理方法
iOS中主要靠NSHTTPCookieStorage和NSHTTPCookie來(lái)管理Cookie,下面我們就來(lái)詳解iOS App開(kāi)發(fā)中Cookie的管理方法,在最后一部分會(huì)單獨(dú)整理出如何清除Cookie的方法.2016-07-07
IOS開(kāi)發(fā)代碼分享之設(shè)置UISearchBar的背景顏色
在項(xiàng)目開(kāi)發(fā)中,我們經(jīng)常要用到UISearchBar,在網(wǎng)上看到了很多關(guān)于去除掉他背景色的方法,都已經(jīng)失效了,今天來(lái)分享一個(gè)正常使用的方法,希望能幫到大家2014-09-09

