IOS 應用內顯示 AppStore 某個應用的詳情_IOS

來源:互聯網
上載者:User

前言

  應用內跳轉到 AppStore 的文章很多,一般都是用 SKStoreProductViewController 來實現的,不知道有沒有在意一個問題:開啟很慢!!怎麼忍?!

 本文

  一般網上的文章的代碼:

 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) }

實現的效果很好,就是很慢,點擊按鈕調用 openAppStore 要很久才能顯示出介面,就算加一個轉圈效果也很差。原因是因為要去  linkmaker.itunes.apple.com 根據 identifier 尋找連結,仔細看代碼我們會發現 presentViewController 是在尋找到結果才被調用,其實我們可以不用讓介面現出來,雖然時間是一樣的,但是使用者體驗會很好,修改後代碼如下:

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 的方式開啟。

參考:

    http://stackoverflow.com/questions/17871920/odd-behavior-with-skstoreproductviewcontroller

結束:        以上就是對ISO 應用內開啟AppStorn顯示某個應用詳情,有需要的朋友可以參考下。   

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.