IOS 应用内显示 AppStore 某个应用的详情

所属分类: 软件编程 / IOS 阅读数: 1907
收藏 0 赞 0 分享

前言

  应用内跳转到 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显示某个应用详情,有需要的朋友可以参考下。   

更多精彩内容其他人还在看

iOS App中UILabel的自定义及在Auto Layout中的使用

这篇文章主要介绍了iOS App中UILabel的自定义及在Auto Layout中的使用,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS应用开发中UITableView的分割线的一些设置技巧

这篇文章主要介绍了iOS应用开发中UITableView分割线的一些设置技巧,包括消除分割线的方法,示例代码为传统的Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

详解iOS时间选择框

这篇文章主要为大家详细介绍了详解iOS时间选择框,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解iOS App开发中UIViewController的loadView方法使用

这篇文章主要介绍了详解iOS App开发中UIViewController的loadView方法使用,讲解了访问view属性时loadView方法的调用及使用loadView时的一些注意点,需要的朋友可以参考下
收藏 0 赞 0 分享

详解在iOS App中自定义和隐藏状态栏的方法

这篇文章主要介绍了在iOS App中自定义和隐藏状态栏的方法,在顶部时某些状况下即用应用内的状态栏覆盖系统本身的,代码示例为Objective-C语言,需要的朋友可以参考下
收藏 0 赞 0 分享

iOS开发实现音频播放功能

本文给大家分享的是在IOS开发过程中实现音频播放的功能,讲解的十分细致,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

IOS开发实现录音功能

本文给大家分享的是一个IOS开发中实现录音功能的实例,并简单给大家解析一下,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

iOS实现图片轮播效果

这篇文章主要为大家详细介绍了IOS实现图片轮播效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

iOS通过http post上传图片

这篇文章主要介绍了iOS通过http post上传图片的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

IOS框架Spring常用的动画效果

本文给大家介绍的是在IOS开发中常用的动画效果以及自定义转场动画特效的代码,非常的简单实用,有需要的小伙伴可以参考下
收藏 0 赞 0 分享
查看更多