1

I'm working on an app right now, and I'm trying to create a button that opens an app that the user has downloaded. Thanks to previous questions, I got SOME working code (Swift). I have the following.

@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)
    }
}

Basically, I have a button that, when clicked, is supposed to open the Microsoft Power BI app I have on my iPad. Unfortunately, I keep getting the error "This app is not allowed to query for scheme mspbi? The URI I have is straight from Microsoft's website. Thoughts on how to open the app? It always skips the "if" and goes right to "else"

Davis
  • 13
  • 4
  • I'd like to ask a follow up! So I am now correctly opening the Power BI app on my iPad with this button...which I'm really excited. However, I'd love to take it one step further. I want to have the URI/URL to change based on where the user is navigating in the app. For example, if the user is in page 1 of my app, I want the button to direct them to the login page of Power BI. If the user is in page 2 of my app, I want the button to direct them to a project page of Power BI. Thoughts? – Davis Jul 21 '17 at 16:06

2 Answers2

2

You have to add the following lines to your application's Info.plist file to be able to handle app URLs:

<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>
Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
0

You need to add the URL scheme to the app's Info.plist

From the Apple Developer Documentation for canOpenURL(_:):

Important

If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by adding the LSApplicationQueriesSchemes key to your app's Info.plist file. If you call this method for a scheme not declared using that key, this method always returns false, whether or not an appropriate app is installed. To learn more about the key, see LSApplicationQueriesSchemes.

So in Info.plist create a new Array with key LSApplicationQueriesSchemes and add an item with value mspbi.

Paolo
  • 3,825
  • 4
  • 25
  • 41
  • Thank you! I thought that this was only something I could do if I was trying to open my own app. Is that false? – Davis Jul 21 '17 at 15:11
  • It worked! Thank you so much :) – Davis Jul 21 '17 at 15:12
  • @Davis Yes you can do it for any 3rd-party app that defines their URL scheme – Paolo Jul 21 '17 at 15:13
  • @Davis glad to hear it, and welcome to Stack Overflow! I'd appreciate it if you could mark this answer as accepted – Paolo Jul 21 '17 at 15:16
  • 1
    @Davis you need to do add `CFBundleURLTypes` key with appropriate values if you want your own app to be opened from other apps, but you need `LSApplicationQueriesSchemes` if you want to open 3rd party apps from your own app. – Dávid Pásztor Jul 21 '17 at 15:42