- 相關(guān)推薦
IOS 應(yīng)用內(nèi)顯示 AppStore 在某個應(yīng)用的詳情
IOS 應(yīng)用內(nèi)顯示 AppStore 在某個應(yīng)用的詳情
前言
應(yīng)用內(nèi)跳轉(zhuǎn)到 AppStore 的文章很多,一般都是用 SKStoreProductViewController 來實現(xiàn)的,不知道有沒有在意一個問題:打開很慢。≡趺慈?!
正文
一般網(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) }
實現(xiàn)的效果很好,就是很慢,點擊按鈕調(diào)用 openAppStore 要很久才能顯示出界面,就算加一個轉(zhuǎn)圈效果也很差。原因是因為要去 linkmaker.itunes.apple.com 根據(jù) identifier 查找鏈接,仔細看代碼我們會發(fā)現(xiàn) presentViewController 是在查找到結(jié)果才被調(diào)用,其實我們可以不用讓界面現(xiàn)出來,雖然時間是一樣的',但是用戶體驗會很好,修改后代碼如下:
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) }
代碼說明:
不等 loadProductWithParameters 返回直接 presentViewController ,解析失敗再嘗試用 openURL 的方式打開。
【IOS 應(yīng)用內(nèi)顯示 AppStore 在某個應(yīng)用的詳情】相關(guān)文章:
iOS11后臺應(yīng)用刷新怎么設(shè)置04-05
ios9.3如何卸載iphone預(yù)裝應(yīng)用05-05
小米手環(huán)數(shù)據(jù)導(dǎo)入iphone ios8健康應(yīng)用教程05-05
電腦顯示應(yīng)用程序無響應(yīng)怎么辦06-27
Win 8.1應(yīng)用程序顯示無法安裝怎么解決04-30
關(guān)于電腦常顯示應(yīng)用程序錯誤的解決方法04-25
怎么解決ios7.04顯示已越獄但是pp助手顯示未越獄的問題03-21
Win8.1應(yīng)用商店如何在任務(wù)欄顯示04-30