您现在的位置是:首页 > 文章详情

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

日期:2017-12-03点击:429

 一般网上的文章的代码:

    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

本文转自博客园农民伯伯的博客,原文链接:【Swift】 应用内显示 AppStore 某个应用的详情,如需转载请自行联系原博主。



原文链接:https://yq.aliyun.com/articles/327391
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章