美文网首页
App内下载应用

App内下载应用

作者: 小凡凡520 | 来源:发表于2020-02-17 14:43 被阅读0次
一、说明

从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)
    }
}

相关文章

网友评论

      本文标题:App内下载应用

      本文链接:https://www.haomeiwen.com/subject/prvtfhtx.html