Swift程序在我的iPad上打开下载的应用程序

时间:2017-07-21 15:03:25

标签: ios swift ipad button

我正在开发一个应用程序,我正在尝试创建一个按钮,用于打开用户已下载的应用程序。感谢以前的问题,我得到了一些工作代码(Swift)。我有以下内容。

@IBAction func Website(_ sender: Any) {

    let powerHooks = "mspbi://app/"

    let powerUrl = NSURL(string: powerHooks)
    if UIApplication.shared.canOpenURL(powerUrl! as URL)
    {
        UIApplication.shared.openURL(powerUrl! as URL)

    } else {
        //redirect to safari because the user doesn't have Power BI
        UIApplication.shared.openURL(NSURL(string: "http://powerbi.microsoft.com")! as URL)
    }
}

基本上,我有一个按钮,当点击它时,应该打开我在iPad上的Microsoft Power BI应用程序。不幸的是,我一直收到错误“这个应用程序不允许查询方案mspbi?我的URI直接来自微软的网站。关于如何打开应用程序的想法?它总是跳过”if“并向右转到”else “

2 个答案:

答案 0 :(得分:1)

您必须在应用程序的Info.plist文件中添加以下行才能处理应用网址:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>mspbi</string> //not sure if this is the correct entry, you will have to check for the specific app
    <string>uber</string>  //as an example, this is the working entry for opening the Uber app from your app
</array>

答案 1 :(得分:0)

您需要将URL方案添加到应用的Info.plist

来自canOpenURL(_:)的{​​{3}}:

  

重要

     

如果您的应用在iOS 9.0上或之后进行了关联,则必须声明   要传递给此方法的URL方案。这样做是通过添加   应用LSApplicationQueriesSchemes文件的Info.plist键。如果你   对于未使用该键声明的方案调用此方法,此方法   无论是否安装了适当的应用程序,始终返回false。   要了解有关密钥的更多信息,请参阅Apple Developer Documentation

因此,在Info.plist中创建一个包含密钥Array的新LSApplicationQueriesSchemes,并添加一个值为mspbi的项目。