iOS9: canOpenURL returning false for WhatApp's url scheme
Asked Answered
T

5

14

In my current project I need to share text on whatsapp from iOS app.

Here is my code to share text on whatsapp:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

but canOpenURL always returning false in iOS9.

Is there any iOS9 privacy policy? Can anyone help me out?

Tintype answered 17/12, 2015 at 8:12 Comment(3)
with iOS 9, Apple Transport Security was introduced. Maybe it has something to do with it. It basically says you can´t send any unsecure requests anymore. But I´m not sure if it is the problem in this case.Chung
maybe this can help you #30988486Chung
ATS has nothing to do with this. Apple introduced a separate system which requires stating in info.plist which schemes an app will use, but it's completely orthogonal to ATS.Superior
S
62

In iOS 9 you must whitelist any URL schemes your App wants to query in Info.plist under the LSApplicationQueriesSchemes key (an array of strings):

For example:

enter image description here

So in your case, instead of fb and twitter you will have to specify whatsapp.

Note that this mechanism only applies to canOpenURL and not openURL. You do not need to have a scheme listed in Info.plist to be able to open it with openURL.

Saez answered 17/12, 2015 at 9:32 Comment(3)
@MayankModi Glad to help :)Saez
For the lazy ones: here is the code for your plist (add it at the end of the main dict node): <key>LSApplicationQueriesSchemes</key> <array> <string>whatsapp</string> </array>Hanson
<key>LSApplicationQueriesSchemes</key> <array> <string>whatsapp</string> <string>fb</string> <string>twitter</string> </array> will it be like that @JuanFranJimenez ?Seattle
S
5

In addition to @z22's answer if you need to add it textually (e.g. with Xamarin) then it looks like this:

    <dict>
        ... 

        <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>whatsapp</string>
        </array>
    </dict>
Swarth answered 29/5, 2017 at 12:41 Comment(0)
R
1

Step:1 Add LSApplicationQueriesSchemes in Info.plist

Step:2 Add whatsapp in items

Rainer answered 18/12, 2015 at 10:9 Comment(0)
S
1

For me the issue was because I was using URL types instead of LSApplicationQueriesSchemes

and it work only for LSApplicationQueriesSchemes

This will not work

URL types

This will work

LSApplicationQueriesSchemes

Stentor answered 12/9, 2017 at 16:25 Comment(0)
P
1

for someone who still finding this issue, its because canOpenURL is abuse to check all installed app. use Open instead and wait for async result.

UIApplication.shared.open(url, options: [:], completionHandler: {success in
            if !success { self.screen?.showToast(message: "App not installed")}
        })
Phoenician answered 27/4, 2023 at 10:0 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.