【Swift】 应用内显示 AppStore 某个应用的详情
一般网上的文章的代码:
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 某个应用的详情,如需转载请自行联系原博主。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
【Swift 2.1】为 UIView 添加点击事件和点击效果
一、为 UIView 添加点击事件 extension UIView { func addOnClickListener(target: AnyObject, action: Selector) { let gr = UITapGestureRecognizer(target: target, action: action) gr.numberOfTapsRequired = 1 userInteractionEnabled = true addGestureRecognizer(gr) } } 二、为 UIView 添加点击效果 class UIViewEffect : UIView { override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { backgroundColor = UIColor.groupTableViewBackgroundColor() } override func touchesCancelled(touches: Set<UITouch&g...
- 下一篇
【Swift】iOS开发笔记(一)
1、隐藏/显示密码功能 光设置secureTextEntry还不行,你会发现UITextField在切换到显示密码时会多一个空字符,看着巨别扭,需要在更改secureTextEntry后进行如下设置: letpwd=psdField.text self.psdField.text=pwd+ " " self.psdField.text=pwd 2、获取当前类的名称 String.fromCString(object_getClassName(self)) 注意:通过_stdlib_getDemangledTypeName也能取到,但是如果在父类里面去就只能取到父类的名称 3、 国际化 find.\(-name'*.m'-o-name'*.h'\)-print0|xargs-0genstrings-oen.lproj 凡是使用了NSLocalizedString的字符串都能被找到,支持子目录查找,注意替换en.lproj 4、UITableView分割线的显示问题 去掉分割线:设置UITableView的separatorStyle =UITableViewCellSepara...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS关闭SELinux安全模块
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS8安装Docker,最新的服务器搭配容器使用
- CentOS8安装MyCat,轻松搞定数据库的读写分离、垂直分库、水平分库
- Linux系统CentOS6、CentOS7手动修改IP地址
- SpringBoot2整合Redis,开启缓存,提高访问速度
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- SpringBoot2全家桶,快速入门学习开发网站教程