通过按钮打开AppStore

Lin*_*rth 35 app-store swift

你能帮助我将以下代码翻译成Swift吗?

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4"]];
Run Code Online (Sandbox Code Playgroud)

(或者我必须使用此链接:itms://itunes.apple.com/app/id839686104?)

提前致谢!

Ato*_*mix 65

这里.但我强烈建议你学习Swift的基础知识!

UIApplication.sharedApplication().openURL(NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")!)
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考,这将打开 *iTunes Store* 应用程序而不是 *App Store*(那么它应该是`itms-apps://`) (5认同)

Stu*_*tto 57

Swift 3语法并改进了'if let'

if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
UIApplication.shared.canOpenURL(url){
    UIApplication.shared.openURL(url)
}
Run Code Online (Sandbox Code Playgroud)

更新7/5/17(感谢奥斯卡指出这一点):

if let url = URL(string: "itms-apps://itunes.apple.com/app/id1024941703"),
    UIApplication.shared.canOpenURL(url)
{
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 很好,但不推荐使用openURL().现在我们应该使用这个行为UIApplication.shared.open(url,options:[:],completionHandler:nil) (5认同)

djd*_*nce 17

我使用这种组合,它更适合速率/购物.

(部分来自这里)

    @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)
}
Run Code Online (Sandbox Code Playgroud)

不要忘记导入和委托:

import StoreKit

class RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate {
Run Code Online (Sandbox Code Playgroud)


And*_*rej 11

由于其他答案对我不起作用(Swift,Xcode 6.1.1),我在这里发布我的解决方案:

var url  = NSURL(string: "itms://itunes.apple.com/de/app/x-gift/id839686104?mt=8&uo=4")

if UIApplication.sharedApplication().canOpenURL(url!) {
    UIApplication.sharedApplication().openURL(url!)
}
Run Code Online (Sandbox Code Playgroud)


Har*_*kar 11

对于 Swift 5(测试代码)打开 App Store 链接

if let url = URL(string: "https://itunes.apple.com/in/app/your-appName/id123456?mt=8") 
{
           if #available(iOS 10.0, *) {
              UIApplication.shared.open(url, options: [:], completionHandler: nil)
           }
           else {
                 if UIApplication.shared.canOpenURL(url as URL) {
                    UIApplication.shared.openURL(url as URL)
                }
           }
} 
Run Code Online (Sandbox Code Playgroud)


mob*_*cat 9

带完成处理程序的Swift 4:

确保在appStoreUrlPath中更新您的ID

func openAppStore() {
    if let url = URL(string: "itms-apps://itunes.apple.com/app/id..."),
        UIApplication.shared.canOpenURL(url){
        UIApplication.shared.open(url, options: [:]) { (opened) in
            if(opened){
                print("App Store Opened")
            }
        }
    } else {
        print("Can't Open URL on Simulator")
    }
}
Run Code Online (Sandbox Code Playgroud)


use*_*695 7

斯威夫特 5.0

导入 StoreKit

extension YourViewController: SKStoreProductViewControllerDelegate {
    func openStoreProductWithiTunesItemIdentifier(_ identifier: String) {
        let storeViewController = SKStoreProductViewController()
        storeViewController.delegate = self

        let parameters = [ SKStoreProductParameterITunesItemIdentifier : identifier]
        storeViewController.loadProduct(withParameters: parameters) { [weak self] (loaded, error) -> Void in
            if loaded {
                self?.present(storeViewController, animated: true, completion: nil)
            }
        }
    }
    private func productViewControllerDidFinish(viewController: SKStoreProductViewController) {
        viewController.dismiss(animated: true, completion: nil)
    }
}

// How to use

openStoreProductWithiTunesItemIdentifier("12345")
Run Code Online (Sandbox Code Playgroud)