【Swift】 应用内显示 AppStore 某个应用的详情

 

 

前言app

  应用内跳转到 AppStore 的文章不少,通常都是用 SKStoreProductViewController 来实现的,不知道有没有在乎一个问题:打开很慢!!怎么忍?!ide

 

声明
  欢迎转载,但请保留文章原始出处:)
  博客园:http://www.cnblogs.com
  农民伯伯: http://over140.cnblogs.comurl

 

正文spa

  通常网上的文章的代码:code

    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 是在查找到结果才被调用,其实咱们能够不用让界面现出来,虽然时间是同样的,可是用户体验会很好,修改后代码以下:blog

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

    代码说明:ci

      不等 loadProductWithParameters 返回直接 presentViewController ,解析失败再尝试用 openURL 的方式打开。博客

 

  参考:string

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

 

结束

  很早以前写过这个功能,因为用户体验很差代码直接被 revert 掉了,今天又搜了一下找到了办法。

相关文章
相关标签/搜索