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

 

 

前言

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

 

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

 

正文

  一般网上的文章的代码:

复制代码
    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

 

结束

  很早之前写过这个功能,由于用户体验不好代码直接被 revert 掉了,今天又搜了一下找到了办法。

posted @   农民伯伯  阅读(2161)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· Open-Sora 2.0 重磅开源!
历史上的今天:
2014-10-30 【Android】用MediaRecorder录制视频太短崩的问题
2014-10-30 【VLC-Android】LibVLC API简介(相当于VLC的MediaPlayer)
2014-10-30 【Android疑难杂症】GridView动态设置Item的宽高导致第一个Item不响应或显示不正常的问题
2009-10-30 浅析简历——中华英才网
点击右上角即可分享
微信分享提示