手机
当前位置:查字典教程网 >编程开发 >IOS开发 >IOS 应用内显示 AppStore 某个应用的详情
IOS 应用内显示 AppStore 某个应用的详情
摘要:前言应用内跳转到AppStore的文章很多,一般都是用SKStoreProductViewController来实现的,不知道有没有在意一...

前言

应用内跳转到 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 应用内显示 AppStore 某个应用的详情】相关文章:

简介iOS开发中应用SQLite的模糊查询和常用函数

IOS应用内跳转系统设置相关界面的方法

实例解析iOS应用多线程开发中NSthread类的用法

iOS应用开发中使用NSLocale类实现对象信息的本地化

iOS实现富文本编辑器的方法详解

IOS实现圆形图片效果的两种方法

iOS中的应用启动原理以及嵌套模型开发示例详解

IOS开发:提示框的正确实现方式

IOS 实现摇一摇的操作

iOS应用开发中使用设计模式中的观察者模式的实例

精品推荐
分类导航