简体   繁体   English

iOS9:canOpenURL 为 WhatApp 的 url 方案返回 false

[英]iOS9: canOpenURL returning false for WhatApp's url scheme

In my current project I need to share text on whatsapp from iOS app.在我当前的项目中,我需要从 iOS 应用程序在 whatsapp 上共享文本。

Here is my code to share text on whatsapp:这是我在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.但是canOpenURL在 iOS9 中总是返回 false。

Is there any iOS9 privacy policy?是否有任何 iOS9 隐私政策? Can anyone help me out?谁能帮我吗?

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):在 iOS 9 中,您必须将您的应用程序想要在 Info.plist 中的 LSApplicationQueriesSchemes 键(字符串数组)下查询的任何 URL 方案列入白名单:

For example:例如:

在此处输入图片说明

So in your case, instead of fb and twitter you will have to specify whatsapp .因此,在您的情况下,您必须指定whatsapp而不是fbtwitter

Note that this mechanism only applies to canOpenURL and not openURL.请注意,此机制仅适用于 canOpenURL 而不适用于 openURL。 You do not need to have a scheme listed in Info.plist to be able to open it with openURL.您无需在 Info.plist 中列出方案即可使用 openURL 打开它。 Hope that helps.希望有帮助。

In addition to @z22's answer if you need to add it textually (eg with Xamarin) then it looks like this:除了@z22 的答案之外,如果您需要以文本方式添加它(例如使用 Xamarin),那么它看起来像这样:

    <dict>
        ... 

        <key>LSApplicationQueriesSchemes</key>
        <array>
            <string>whatsapp</string>
        </array>
    </dict>

Step:1 Add LSApplicationQueriesSchemes in Info.plist步骤:1 在 Info.plist 中添加 LSApplicationQueriesSchemes

Step:2 Add whatsapp in items步骤:2 在项目中添加 whatsapp

For me the issue was because I was using URL types instead of LSApplicationQueriesSchemes对我来说,问题是因为我使用的是URL 类型而不是LSApplicationQueriesSchemes

and it work only for LSApplicationQueriesSchemes它仅适用于LSApplicationQueriesSchemes

This will not work行不通

网址类型

This will work这将工作

LSApplicationQueriesSchemes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM