一、说明
从iOS6以后苹果提供了在应用内部打开App Store中某一个应用下载页面的方式,提供了一个SKStoreProductViewController的类对该功能进行支持。
二、代码
import UIKit
import StoreKit
class TestViewController: UIViewController,SKStoreProductViewControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func action(_ sender: Any) {
let appid = 1401729613
let storevc = SKStoreProductViewController()
storevc.delegate = self
storevc.view.frame = self.view.frame
if #available(iOS 11.0, *) {
let para = [
SKStoreProductParameterITunesItemIdentifier:NSNumber(value: appid)
]
storevc.loadProduct(withParameters: para) { (result, error) in
if result {
self.present(storevc, animated: true, completion: nil)
} else {
print(error)
}
}
} else {
let string = "itms-apps://itunes.apple.com/xxxxxxx/app/id\(appid)?mt=8"
if let url = URL(string: string) {
UIApplication.shared.openURL(url)
}
}
}
func productViewControllerDidFinish(_ viewController: SKStoreProductViewController) {
self.dismiss(animated: true, completion: nil)
}
}
网友评论