在swift中打开特定数字的Whatsapp

时间:2016-11-10 19:14:09

标签: ios whatsapp

我正在尝试在whatsapp中打开特定的联系人聊天,但没有得到任何解决方案。请帮助我完全陷入困境。我试过这个:

let whatsAppURL: NSURL = NSURL(string: "whatsapp://send?abid=\(primary)&;text=lOL;")!
        if UIApplication.sharedApplication().canOpenURL(whatsAppURL){
            UIApplication.sharedApplication().openURL(whatsAppURL)
        }

8 个答案:

答案 0 :(得分:29)

可能您可以向Specfic用户发送消息。

直接应用聊天网址

let urlWhats = "whatsapp://send?phone=+919789384445&abid=12354&text=Hello"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = URL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL!) {
                UIApplication.shared.openURL(whatsappURL!)
            } else {
                print("Install Whatsapp")
            }
        }
    }
  

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

的必填项

WebUrl链接打开聊天

 let whatsappURL = URL(string: "https://api.whatsapp.com/send?phone=9512347895&text=Invitation")
    if UIApplication.shared.canOpenURL(whatsappURL) {
        UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
    }

检查以下链接,

https://www.whatsapp.com/faq/en/general/26000030

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

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

答案 1 :(得分:12)

适用于Swift 2.0

let urlWhats = "whatsapp://send?phone=(mobile number with country code)"
        if let urlString = urlWhats.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet()){
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.sharedApplication().canOpenURL(whatsappURL){
                    UIApplication.sharedApplication().openURL(whatsappURL)
                }
                else {
                    print("Install Whatsapp")
                }
            }
        }

适用于swift 4.2 / Swift 5

func openWhatsapp(){
    let urlWhats = "whatsapp://send?phone=(mobile number with country code)"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed){
        if let whatsappURL = URL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL){
                if #available(iOS 10.0, *) {
                    UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
                } else {
                    UIApplication.shared.openURL(whatsappURL)
                }
            }
            else {
                print("Install Whatsapp")
            }
        }
    }
}

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

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

答案 2 :(得分:4)

根据此whatsapp forum link,您无法向特定用户发送消息,这在whatsapp URL方案中不可用。

您只需设置预定义消息,然后使用URL方案即可打开whatsapp最近的控制器。

答案 3 :(得分:2)

您应该尝试...

guard let url = URL(string: "https://wa.me/your_number") else {
        return //be safe
    }
    if #available(iOS 10.0, *) {
        UIApplication.shared.open(url, options: [:], completionHandler: nil)
    } else {
        UIApplication.shared.openURL(url)
    }

答案 4 :(得分:2)

这对我很有效

    if let url = URL(string: "https://wa.me/91xxxxxxxxxx?text=Hello"),
            UIApplication.shared.canOpenURL(url) {
                UIApplication.shared.open(url, options: [:])
    }

答案 5 :(得分:1)

这是不可能的,你可以用URL方案打开WhatsApp。

答案 6 :(得分:0)

这个问题真的很有帮助

只需输入不带“ +”的人数 例如:60161234567

答案 7 :(得分:0)

  1. 将属性添加到_onPhoneNumberPressed = async () => { try { const phoneNumber = await SmsRetriever.requestPhoneNumber(); this.setState({phone: phoneNumber.split('+91')[1]}); } catch (error) { console.log(JSON.stringify(error)); }} 源代码文件中,如下所示:

    Info.plist
  2. 在应用中的自定义点击上使用以下功能:

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