Open whatsapp on iOS with new phone number pre-filled

我怕爱的太早我们不能终老 提交于 2019-11-27 21:53:50

问题


I know there is scheme to communicate with whatsapp like:

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?text=Hello%2C%20World!"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

But i can't find how to open whatsapp with a phone number that don't exist before.

It's for a "contatc us" page and if i can only open whatsapp and can't pre fill a phone number, it's useless. I need to be contacted with whatsapp...

Does what I am trying to do exist ?


回答1:


For phone number pre-filled And Text

   NSURL *whatsappURL = [NSURL URLWithString:@"https://api.whatsapp.com/send?phone=9530670491&text=hello"];
if ([[UIApplication sharedApplication] canOpenURL: whatsappURL]) {
    [[UIApplication sharedApplication] openURL: whatsappURL];
}

click here for Swift

Check Link For More Details https://www.whatsapp.com/faq/en/general/26000030




回答2:


According to the Whatsapp documentation, you need to actually have the contact in your address book to open a discussion from the url scheme.
So for your question

Does what i try to do exist ?

the answer is: No.




回答3:


You can find the documentation of that whatsapp API above answer. Adding some more point in that based on my own trial :

For communicating direct with the specific contact you need abid of that contact. ABID is that parameter which is automatically generated by the system when you save the contact. So if you dont have number saved in your contact so no way you can open that from your application .

There is one workaround that I have used. When your application load first time in the device , you save the contact us number to the address book . when number saved successfully you will get ABID in the return . you can use that ABID to invoke messaging with that contact.

NSURL *whatsappURL = [NSURL URLWithString:@"whatsapp://send?abid=123&text=Hello%2C%20World!"];



回答4:


If anyone looking for Swift version

static func triggerWhats(_ vc:UIViewController){
        let msg = "Hello! I have a question"
        let urlWhats = "whatsapp://send?phone=+6599999999&text=\(msg)"
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed){
            if let whatsappURL = NSURL(string: urlString) {
                if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                    _ = UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: nil)
                } else {
                    // Cannot open whatsapp
                    ViewUtil().popAlertView(vc, title: "WhatsApp Support", message: "Please WhatsApp us at +65999999 if you have any questions", option: "Ok")
                }
            }
        }
    }



回答5:


Swift 3.0 open whats app.

in info.plist file add this line

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

func OpenWhatsApp(_ vc:UIViewController){
        let msg = "" // here pass your message 
        let urlWhats = "https://api.whatsapp.com/send?phone=(Mobileno)&text=\(msg)" // Mobile number that include country code and without + sign . 
        if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: .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 {
                        // Fallback on earlier versions
                        UIApplication.shared.canOpenURL(whatsappURL)
                    }
                } else {
                    // Cannot open whatsapp
                    AppUtilities.sharedInstance.showAlert(title: "WhatsApp Support", msg: "Your device is not support whats app")
                }
            }
        }
    }



回答6:


For Swift 3.0 OR Above, This will open whatsapp chat of a number

Example of Number: "+918798766554"

if let whatappURL = URL(string: "https://api.whatsapp.com/send?phone=\(number)&text=\(msg)"),
  UIApplication.shared.canOpenURL(whatappURL)
{
  if #available(iOS 10.0, *) {
    UIApplication.shared.open(whatappURL, options: [:], completionHandler: nil)
  } else {
    UIApplication.shared.openURL(whatappURL)
  }
}


来源:https://stackoverflow.com/questions/31202199/open-whatsapp-on-ios-with-new-phone-number-pre-filled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!