【问题标题】:Swift - Opening another app in else statementSwift - 在 else 语句中打开另一个应用程序
【发布时间】:2020-04-05 19:37:57
【问题描述】:

我想从我的应用中打开优步应用,此时分数将高于 25。这是我的代码:

        scoreLabel.text = "\(score)"


        if score <= 5
        { promilLabel.text = "Trzezwy"
        }
        else if score <= 10{
            promilLabel.text = "Wesoly"
        }
        else if score <= 15{
            promilLabel.text = "Pijany"
        }
        else if score <= 25{
             promilLabel.text = "Napierdolony"
        }
        else{
            promilLabel.text = "Wytrzezwialka"
        }
    }

我找到了这样的东西,但是如何将它添加到else语句中?:

if let url = NSURL(string: "app://") where UIApplication.sharedApplication().canOpenURL(url) {
            UIApplication.sharedApplication().openURL(url)
} else if let itunesUrl = NSURL(string: "https://itunes.apple.com/itunes-link-to-app") where UIApplication.sharedApplication().canOpenURL(itunesUrl) {
            UIApplication.sharedApplication().openURL(itunesUrl)      
}

【问题讨论】:

    标签: ios swift if-statement url-scheme


    【解决方案1】:

    创建一个类似的函数:

    func callUrl(){
    if let url = NSURL(string: "app://") where UIApplication.sharedApplication().canOpenURL(url) {
            UIApplication.sharedApplication().openURL(url)
    } else if let itunesUrl = NSURL(string: "https://itunes.apple.com/itunes-link-to-app") where UIApplication.sharedApplication().canOpenURL(itunesUrl) {
            UIApplication.sharedApplication().openURL(itunesUrl)      
    }
    }
    

    并在 else 中调用此函数。

    else{
            promilLabel.text = "Wytrzezwialka"
            self.callUrl()
    }
    

    【讨论】:

    • 一切正常,但不是打开超级应用程序而是直接进入应用程序商店。有什么想法吗?
    • 我上面的结构很好,尝试更改您的 URL。我回答了你的问题。谢谢!
    • 谢谢! :))) 仍然无法打开设备上安装的应用程序。它直接将我重定向到 itunesUrl
    • @PawelZet 请接受它。如果对你有帮助。万事如意(Y)
    【解决方案2】:

    它是这样工作的:

    func callUrl(){
              if let url = NSURL(string: "uber://"), UIApplication.shared.canOpenURL(url as URL) {
                  UIApplication.shared.openURL(url as URL)
              } else if let itunesUrl = NSURL(string: "https://itunes.apple.com/itunes-link-to-app"), UIApplication.shared.canOpenURL(itunesUrl as URL) {
                  UIApplication.shared.openURL(itunesUrl as URL)
                }
                }
    

    在其他声明中刚刚添加:

    self.callUrl()
    

    【讨论】:

    • 你复制了我的回答并发布在这里。
    • 这不是你的答案。我已经改变了一点,这就是我在这里发布它的原因。而且它并不完全有效。
    • 您的问题是“我如何添加到 else 语句中?”
    • 是的,您对此提供了帮助。谢谢你。那是正确的。但是我已经稍微更改了代码以使其正常工作,这就是为什么我发布了哪个对我有用。现在我知道如何把这个函数放在 else 语句中了。我不知道的一件事是为什么它没有直接打开 uber 应用程序,而是在 else if 中有链接。再次感谢您的帮助
    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多