canOpenURL not working in ios 10 [duplicate]

2019-04-09 08:10发布

问题:

This question already has an answer here:

  • Google Maps URL scheme not working on iOS 9 2 answers

The above code is always returning false

if {(UIApplication.shared.canOpenURL(URL(string:"comgooglemaps://")!))}

I guess this a problem found in iOS10. I am trying to open google maps app if there in one installed or try to open apple maps so wanted to use canOpenURL . are there any alternatives

回答1:

Add this to your Info.plist and then try calling canOpenURL.

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>comgooglemaps</string>
</array>


回答2:

Edit:

The correct key to be used in the app's plist file is LSApplicationQueriesSchemes and not UIDefaultLaunchStoryboard as stated by Apple's documentation.

Original answer:

From Apple's documentation:

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 using the UIDefaultLaunchStoryboard array in your Xcode project’s Info.plist file. For each URL scheme you want your app to use with this method, add it as a string in this array.

If your (iOS 9.0 or later) app calls this method using a scheme you have not declared, the method returns false, whether or not an appropriate app for the scheme is installed on the device.

You can read more about it here.