前言
应用内跳转到 AppStore 的文章很多,一般都是用 SKStoreProductViewController 来实现的,不知道有没有在意一个问题:打开很慢!!怎么忍?!
正文
一般网上的文章的代码:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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 是在查找到结果才被调用,其实我们可以不用让界面现出来,虽然时间是一样的,但是用户体验会很好,修改后代码如下:
?
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
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显示某个应用详情,有需要的朋友可以参考下。
相关文章
猜你喜欢
- 个人网站搭建:如何挑选具有弹性扩展能力的服务器? 2025-06-10
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
thinkphp5 + ajax 使用formdata提交数据(包括文件上传) 后台返回json完整实例
2025-05-27 34 -
2025-05-25 92
-
2025-05-29 63
-
2025-05-25 44
-
2025-05-27 76
热门评论

