Search code examples
iosswiftxcodensurlconnectionfunc

swift - App does not open from another app


I try to open Uber app from my app. It redirects me straight to the appstore, not to the app. I would like it to open Uber app directly not via appstore app. Here is my function:

func callUrl(){
    if let url = NSURL(string: "Uber://"), UIApplication.shared.canOpenURL(url as URL) {
        UIApplication.shared.open(url as URL)
    } else if let itunesUrl = NSURL(string: "https://apps.apple.com/us/app/uber/id368677368"), UIApplication.shared.canOpenURL(itunesUrl as URL) {
        UIApplication.shared.open(itunesUrl as URL)
    }
}

Solution

  • I think the else statement get called because maybe "Uber://" is a not valid key to open the app directly.

    Maybe this will work for you

    if let url = URL(string: "uber://"),
        UIApplication.shared.canOpenURL(url) {
    
        UIApplication.shared.open(url)
    }
    else {
        UIApplication.shared.open(URL(string: "https://apps.apple.com/us/app/uber/id368677368")!)
    }
    

    EDIT

    Add LSApplicationQueriesSchemes key to Info.plist with uber value

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>uber</string>
    </array>
    

    Uber Documentation