将Whatsapp消息发送到特定的联系号码(Swift Project)

时间:2016-09-25 12:48:39

标签: swift url social-networking whatsapp

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

使用这个简单的代码:

  {% for event in events %}

    <tr>
        <td>
            <input type="checkbox" name="events[]" value="{{ event.id }}"/>
        </td>
        <td>
            <input style="border: 1px solid lightblue;" type="text" name="title[{{ event.id }}]" value="{{ event.title}}">
        </td>
        <td>
            <textarea style="border: 1px solid lightblue;" class="mce_ta" type="text" name="description[{{ event.id }}]">{{ event.description }}</textarea>
        </td>
        <td>
            <input style="border: 1px solid lightblue;" type="text" name="source_url[{{ event.id }}]" value="{{ event.source_url }}">
        </td>
        <td>
            <select name="type_id[{{ event.id }}]" class="form-control">
                <option selected value="{{ event.type_id }}">{{ event.type_name }}</option>
                {% for type in types %}
                    <option value="{{ type.id }}">{{ type.type_name }}</option>
                {% endfor %}
            </select>
        </td>
        <td><input class="event_start_date date-picker" type="text" name="event_start_date[{{ event.id }}]" value="{{ event.event_start_date }}"></td>
        <td><input class="event_end_date date-picker" type="text" name="event_end_date[{{ event.id }}]" value="{{ event.event_end_date }}"></td>
        <td><input class="creation_date date-picker" type="text" name="event_date[{{ event.id }}]" value="{{ event.creation_date }}"></td>
        <td><input class="time_pick clockpicker" type="text" name="start_time[{{ event.id }}]" value="{{ event.start_time }}"></td>
        <td><input class="time_pick clockpicker" type="text" name="end_time[{{ event.id }}]" value="{{ event.end_time }}"></td>
        <td>
            <select name="venue_id[{{ event.id }}]" class="form-control">
                <option selected value="{{ event.venue_id }}">{{ event.venue_name }}</option>
                {% for venue in venues %}
                    <option value="{{ venue.id }}">{{ venue.venue_name }}</option>
                {% endfor %}
            </select>
        </td>


    </tr>
{% endfor %}

我总是收到警告信息,这是其他情况! 虽然数字总是如此! 可能是url语法中的错误?

在控制台中:

   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()
        }

这是正确的方法吗? 或者这种方式只是为了分享,通过Whatsapp文本?

3 个答案:

答案 0 :(得分:5)

试试这个......

 let urlWhats = "whatsapp://send?phone=***********&text=***"

 var characterSet = CharacterSet.urlQueryAllowed
  characterSet.insert(charactersIn: "?&")

  if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: characterSet){

  if let whatsappURL = NSURL(string: urlString) {
                    if UIApplication.shared.canOpenURL(whatsappURL as URL){
                        UIApplication.shared.openURL(whatsappURL as URL)
                    }
                    else {
                        print("Install Whatsapp")

                    }
                }
            }
  

注意:国家代码(例如:+91)是打开手机号码聊天

的必填项

注意:在info.plist中添加url方案

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

答案 1 :(得分:4)

两个问题。

首先,这不是一个有效的网址方案。 URL方案采用identifier://params格式,因此您需要使用whatsapp://phone_number

其次,Apple现在要求您定义应用程序在Info.plist文件中使用哪些外部URL方案,这些方案嵌套在关键LSApplicationQueriesSchemes下。有关详细信息,请参阅iOS 9 not opening Instagram app with URL SCHEME

根据Whatsapp网址计划文档,您实际上无法提供您要将邮件发送至https://www.whatsapp.com/faq/en/iphone/23559013的联系人的电话号码。

但是,您可以提供您要发送给他们的消息:

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

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

答案 2 :(得分:1)

如果要发送给特定号码,请使用以下代码:

  let whatsAppUrl = NSURL(string: "https://api.whatsapp.com/send?phone=**********&text=******")



        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()
        }

该技巧之所以奏效,是因为您没有直接调用 whatsapp,而是使用浏览器,并且该浏览器可以接收电话号码,并在所需的电话和所需的文本中打开whatsapp