canOpenURL return true and app is installed but app is not opening

976 Views Asked by At

when I using canOpenURL to open universal link, (target app is installed)

canOpenURL return true, but app is not opening (LSApplicationQueriesSchemes are not registered)

but if I use open(_ url, options) app is opening

If I am responsible for not registering LSApplicationQueriesSchemes, how do I register Universal's links to LSApplicationQueriesSchemes? (https://www.aaa.com)

<key>LSApplicationQueriesSchemes</key>
<array>
<string>https://www.aaa.com</string>
</array>

like this?

1

There are 1 best solutions below

2
valosip On

If AAA is the app that you're trying to open, there is another way to launch it from inside your app. You will need to grab the CFBundleURLSchemes that the target app uses.
For AAA its "aaamobile" For AAA Auto Club its "aaamobileace"

You then need to add the url schemes into your plist:

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>aaamobile</string>
</array>

And then in your code you can check to see if the app is installed and launch the app or take user to website:

    guard let url = URL(string: "aaamobile://") else { return }
    if UIApplication.shared.canOpenURL(url) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.open(URL(string: "https://www.aaa.com")!, options: [:], completionHandler: nil)
    }