将 Whatsapp 消息发送到特定的联系号码(Swift 项目) [英] Sending Whatsapp message to a specific contact number (Swift Project)

查看:70
本文介绍了将 Whatsapp 消息发送到特定的联系号码(Swift 项目)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向存储在全局变量中的收件人号码发送 whatsapp 消息!

I'm trying to send a whatsapp message to a recipient number stored in a global variable!

通过使用这个简单的代码:

By using this simple code:

   let whatsAppUrl = NSURL(string: "whatsapp:\(globalPhone)")



        if UIApplication.shared.canOpenURL(whatsAppUrl as! URL) {
            UIApplication.shared.openURL(whatsAppUrl as! URL)
        }
        else {
            let errorAlert = UIAlertView(title: "Sorry", message: "You can't send a message to this number", delegate: self, cancelButtonTitle:"Ok")
            errorAlert.show()
        }

我总是收到警报消息,这是其他情况!虽然数字永远是真的!可能是url语法错误?

I'm always getting the alert message which's the else case! although the number is always true! May be the the error in the url syntax?

在控制台中:

canOpenURL: failed for URL: "whatsapp:0534260282" -
"This app is not allowed to query for scheme whatsapp"

这是正确的做法吗?或者这种方式只是为了分享,通过Whatsapp发短信?

Is this the correct way to do that? Or this way just for sharing, text through Whatsapp?

推荐答案

两个问题.

首先,这不是有效的 url 方案.URL 方案采用 identifier://params 格式,因此您需要改用 whatsapp://phone_number.

The first is that's not a valid url scheme. A URL scheme takes the format identifier://params so you'll need to use whatsapp://phone_number instead.

其次是 Apple 现在要求您在 Info.plist 文件中定义应用程序使用的外部 url 方案,该文件嵌套在键 LSApplicationQueriesSchemes 下.请参阅 iOS 9 不使用 URL SCHEME 打开 Instagram 应用 了解更多信息.

Secondly is that Apple now requires you to define which external url schemes that your application uses in your Info.plist file, nested under the key LSApplicationQueriesSchemes. Please see iOS 9 not opening Instagram app with URL SCHEME for more info.

根据 Whatsapp URL 方案文档,您实际上无法提供要将消息发送至的联系人的电话号码:https://www.whatsapp.com/faq/en/iphone/23559013.

According to the Whatsapp URL scheme docs, you can't actually supply the phone number of the contact that you'd like to send the message to: https://www.whatsapp.com/faq/en/iphone/23559013.

然而,您可以提供您想发送给他们的消息:

You can however supply the message that you'd like to send to them:

whatsapp://send?text=Some%20Text.

确保文本是百分比编码的,否则 NSURL 将无法从提供的字符串创建有效的 URL.

Ensure that the text is percentage encoded as otherwise NSURL will fail to create a valid URL from the supplied string.

这篇关于将 Whatsapp 消息发送到特定的联系号码(Swift 项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆