通过按钮打开AppStore

时间:2014-09-17 05:30:52

标签: swift app-store

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

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

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

提前致谢!

15 个答案:

答案 0 :(得分:60)

下面。但我强烈建议你学习Swift的基础知识!

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

答案 1 :(得分:53)

Swift 3语法,如果让'

改进了'
var menus = document.querySelectorAll('.menu');

function toggleMenu(e){

    var target = e.target;
    // only care about clicks on parent list items.
    if (target.className.indexOf('parent') === -1) {
        return;
    }
    // use querySelector to find the first <ul> inside this parent <li>
    var submenu = target.querySelector('ul');

    if (submenu) {
        submenu.classList.toggle('active');
    }
}

// add a click handler to each menu
menus.forEach(function(menu) {
    menu.addEventListener('click', toggleMenu);
});

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

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

答案 2 :(得分:17)

我使用这种组合,它更适合费率/购物。

(部分来自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)
}

请勿忘记导入和委派:

import StoreKit

class RateMeViewController: UIViewController, SKStoreProductViewControllerDelegate {

答案 3 :(得分: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!)
}

答案 4 :(得分: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")
    }
}

答案 5 :(得分:4)

  

检查Swift 3中iTunes上的新应用更新

let currentAppVersion = Bundle.main.infoDictionary
Alamofire.request("http://itunes.apple.com/jp/lookup/?id=548615", method: .get, parameters: nil, headers: nil).responseJSON { response in
        if let value = response.result.value as? [String: AnyObject] {
            let versionNum = value["results"]?.value(forKey: "version") as? NSArray
            if versionNum?[0] as! String != currentAppVersion?["CFBundleShortVersionString"] as! String {
                self.alertForUpdateApp()
            }
        }
    }

func alertForUpdateApp() {

    let alertController = UIAlertController(title: "Update Available", message: "There is a newer version of this app available", preferredStyle: .alert)
    let alertActionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
    let alertActionUpdate = UIAlertAction(title: "Update", style: .default, handler: { _ in
        if let url = URL(string: Constants.API_REDIRECT_TO_ITUNES),
            UIApplication.shared.canOpenURL(url){
            UIApplication.shared.open(url, options: [:], completionHandler: nil)

        }
    })

    alertController.addAction(alertActionCancel)
    alertController.addAction(alertActionUpdate)

    let pushedViewControllers = (self.window?.rootViewController as! UINavigationController).viewControllers
    let presentedViewController = pushedViewControllers[pushedViewControllers.count - 1]

    presentedViewController.present(alertController, animated: true, completion: nil)

}

答案 6 :(得分:1)

    var url  = NSURL(string: "itms-apps://itunes.apple.com/app/id1024941703")
    if UIApplication.sharedApplication().canOpenURL(url!) == true  {
        UIApplication.sharedApplication().openURL(url!)
    }

Xcode 6.4

答案 7 :(得分:1)

对于新的AppStore,只需在AppStore上打开应用程序的链接,然后将https方案替换为itms-apps方案。 Swift 4上的示例:

if let url = URL(string: "itms-apps://itunes.apple.com/us/app/my-app/id12345678?ls=1&mt=8") {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}

您可以在“应用信息”页面上找到应用的链接。

答案 8 :(得分:1)

如果要在应用商店中打开,请使用

<i class="material-icons help_icon">info_outline</i>

如果要在iTunes商店中使用

 let appstoreUrl =  "https://itunes.apple.com/in/app/myapp-test/id11111111?mt=8"
 UIApplication.shared.openURL(URL(string: appstoreUrl)!)

答案 9 :(得分:1)

雨燕5

if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
 } 
else {
    // Earlier versions 
    if UIApplication.shared.canOpenURL(url as URL) {
        UIApplication.shared.openURL(url as URL)
    }
}

答案 10 :(得分:1)

Swift 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")

答案 11 :(得分:1)

SwiftUI

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

namespace SomeNamespace.Properties {
    
    
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.7.0.0")]
    public sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
        
        private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
        
        public static Settings Default {
            get {
                return defaultInstance;
            }
        }
        
        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("a;b;c")]
        public global::SomeNamespace.Part Part {
            get {
                return ((global::SomeNamespace.Part)(this["Part"]));
            }
            set {
                this["Part"] = value;
            }
        }
    }
}

//使用方法

import StoreKit

struct StoreView: UIViewControllerRepresentable {

    let appID: String
 
     
    func makeUIViewController(context: UIViewControllerRepresentableContext<StoreView>) -> SKStoreProductViewController {
        let sKStoreProductViewController = SKStoreProductViewController()
        let parameters = [ SKStoreProductParameterITunesItemIdentifier : appID]
        sKStoreProductViewController.loadProduct(withParameters: parameters)
        return sKStoreProductViewController
    }

    func updateUIViewController(_ uiViewController: SKStoreProductViewController, context: UIViewControllerRepresentableContext<StoreView>) {

    }

}

答案 12 :(得分:0)

对我来说,此答案之一无效。 (快速4)应用程序始终会打开iTunes Store,而不是打开AppStore。

我必须按照此苹果问答中所述将网址更改为“ http://appstore.com/%Appname%”:https://developer.apple.com/library/archive/qa/qa1633/_index.html

例如这样的

private let APPSTORE_URL = "https://appstore.com/keynote"
UIApplication.shared.openURL(URL(string: self.APPSTORE_URL)!)

(从应用名称中删除空格)

答案 13 :(得分:0)

Swift 4。** 2和** Xcode 10.2

您可以通过两种方式打开 App Store iTunes Store

如果要打开App Store,请使用 https ;如果要打开iTunes Store,请使用 itms

例如:https://itunes.apple.com/in/app/yourAppName/id14 **** 4?mt = 8 //对于App Store

itms://itunes.apple.com/in/app/yourAppName/id14****4?mt = 8 //对于iTu​​nes Store

方法1:这是简单直接的旧方法

<TwitterLogin
    //localhost:4000 endpoint should be your api running url
    loginUrl="http://localhost:4000/api/v1/auth/twitter"
    onFailure={this.onFailed}
    onSuccess={this.onSuccess}
    //your api endpoint
    requestTokenUrl="http://localhost:4000/api/v1/auth/twitter/reverse"
/>

方法2:新方法

let url  = NSURL(string: "https://itunes.apple.com/in/app/smsdaddy/id1450172544?mt=8")//itms   https
    if UIApplication.shared.canOpenURL(url! as URL) {
        UIApplication.shared.openURL(url! as URL)
    }

答案 14 :(得分:0)

对于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)
                }
           }
}