IOS - Can't open Whatsapp chat programatically, but can do it through HTML

只愿长相守 提交于 2019-12-01 06:34:33

问题


I have a Web page with an anchor that has href="whatsapp:+(xxxxxxxxx)", if i click on it on my iPhone (iOS 9), it opens WhatsApp in the chat view for that contact (i can see all the chats that i did with the contact). However, if i do it programmatically (same url), it only opens WhatsApp on the chat tab.

I've tried other ways of doing this (using the Address Book api, and using the "whatsapp://send?abid=RECORDID"), but all fail.

Any help will be much appreciated.


回答1:


You should add the WhatsApp URL scheme to your application Info.plist

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

According to Apple Developer documentation

IMPORTANT If your app is linked on or after iOS 9.0, you must declare the URL schemes you want to pass to this method. Do this by using the LSApplicationQueriesSchemes array in your Xcode project’s Info.plist file. For each URL scheme you want your app to use with this method, add it as a string in this array.

If your (iOS 9.0 or later) app calls this method using a scheme you have not declared, the method returns NO, whether or not an appropriate app for the scheme is installed on the device.




回答2:


working fine with xcode 8.3.2 and swift 3

    let date = Date()
    let msg = "Hi my dear friends\(date)"
    let urlWhats = "whatsapp://send?phone=phoneNumber&abid=phoneNumber&text=hi"
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                UIApplication.shared.openURL(whatsappURL as URL)
            } else {
                print("please install watsapp")
            }
        }
    }


来源:https://stackoverflow.com/questions/36066770/ios-cant-open-whatsapp-chat-programatically-but-can-do-it-through-html

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