Open AppStore through button

前端 未结 15 750
逝去的感伤
逝去的感伤 2020-12-12 22:18

Could you guys help me to translate the following code into Swift?

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@\"itms://itunes.apple.com         


        
15条回答
  •  粉色の甜心
    2020-12-12 22:35

    I use this combination, its better for rate/shopping.

    (partially from here)

        @IBAction func rateMe(sender: AnyObject) {
        if #available(iOS 8.0, *) {
            openStoreProductWithiTunesItemIdentifier("107698237252");
        } else {
            var url  = NSURL(string: "itms://itunes.apple.com/us/app/xxxxxxxxxxx/id107698237252?ls=1&mt=8")
            if UIApplication.sharedApplication().canOpenURL(url!) == true  {
                UIApplication.sharedApplication().openURL(url!)
            }
    
        }
    }
    func openStoreProductWithiTunesItemIdentifier(identifier: String) {
        let storeViewController = SKStoreProductViewController()
        storeViewController.delegate = self
    
        let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
        storeViewController.loadProductWithParameters(parameters) { [weak self] (loaded, error) -> Void in
            if loaded {
                // Parent class of self is UIViewContorller
                self?.presentViewController(storeViewController, animated: true, completion: nil)
            }
        }
    }
    func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
        viewController.dismissViewControllerAnimated(true, completion: nil)
    }
    

    don't forget to import and delegate:

    import StoreKit
    
    class RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate {
    

提交回复
热议问题