在 swift 中的特定号码上打开 Whatsapp

Open Whatsapp on a particular number in swift

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

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

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

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

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

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

直接应用聊天url打开

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

Note:Country code (Ex:+91) is mandatory to open mobile number Chat

WebUrl Link 打开聊天

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

检查下方 link,

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

注意:在info.plist

中添加url方案
<key>LSApplicationQueriesSchemes</key>
 <array>
    <string>whatsapp</string>
 </array>

对于swift4.2/Swift5

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

为Swift2.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")
                }
            }
        }

注:在info.plist

中添加url方案
<key>LSApplicationQueriesSchemes</key>
 <array>
    <string>whatsapp</string>
 </array>

这对我来说很有效

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

这个问题很有帮助

只填人数不带“+” 示例:60161234567

  1. 像这样向 Info.plist 源代码文件添加属性:

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>whatsapp</string>
    </array>
    
  2. 在您的应用中对自定义点击使用以下功能:

    func openWhatsapp(){
        let urlWhats = "https://wa.me/numberWithCountryCode"
        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 {
                    Alert(withMessage: "You do not have WhatsApp installed! \nPlease install first.").show(andCloseAfter: 2.0)
                }
            }
        }
    }