当前位置: 移动技术网 > 移动技术>移动开发>IOS > IOS 应用内显示 AppStore 某个应用的详情

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

前言

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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网