【问题标题】:How to open WhatsApp from Swift app?如何从 Swift 应用程序打开 WhatsApp?
【发布时间】:2017-02-10 02:24:30
【问题描述】:

我在我的 Swift 应用程序中使用 webview,并且我的网站上有“在 WhatsApp 上分享”按钮,该按钮在浏览器上运行良好。但是在 iPhone 应用上,当我点击按钮时,什么也没有发生。

如何从我的应用程序中打开 WhatsApp?我正在使用 Xcode 8 和 iOS 10。

【问题讨论】:

    标签: ios swift whatsapp


    【解决方案1】:

    为此,您应该使用 URL 方案。

    let message = "Message"
    let urlWhats = "whatsapp://send?text=\(message)"
    
    if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
        if let whatsappURL = NSURL(string: urlString) {
            if UIApplication.shared.canOpenURL(whatsappURL as URL) {
                UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: { (Bool) in
    
                })
            } else {
                // Handle a problem
            }
        }
    } 
    

    【讨论】:

    • 感谢您的回复,但它不起作用,我在我的网站上使用了whatsapp分享按钮,所以只想从ios应用打开
    【解决方案2】:

    我知道这是一个老问题,但以下对我有用(我使用的是 xcode 8.3.3 和 swift 3)。

    我在 Info.plist 中添加了 whatsapp 查询方案。

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

    Info.plist

    添加后,以下工作:

    let urlString = "whatsapp://send?text=Message to share"
    
    let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    
    let URL = NSURL(string: urlStringEncoded!)
    
    if UIApplication.shared.canOpenURL(URL! as URL) {
        UIApplication.shared.openURL(URL! as URL)
    }
    

    【讨论】:

    • 太好了,现在从这里开始,您如何简单地将 .pdf 文件附加到您的示例中?我能找到的所有其他示例都使用 DocumentInteractionsController。
    • @DaWiseguy,你的意思是打开一个 pdf 文件吗?
    • 我的理解是,您也可以通过相同的 urlString 附加 pdf。但经过进一步研究,您似乎必须使用 DocumentInteractionController 将任何文件附加到 Swift 中的 WhatsApp 消息中。
    • 这是直接打开 WhatsApp 还是先打开 safari?你能把电话号码也输入吗?
    • 嗨@JayVDiyk,在这种情况下,如果安装了WhatsApp,它会直接打开它。否则它哪儿也去不了。您可以像这样使用电话:whatsapp://send?phone=+55999999999 将 55 替换为国家代码,其余的替换为电话号码。
    【解决方案3】:
    UIApplication.shared.openURL(URL(string:"https://api.whatsapp.com/send?phone=phoneNumber")!)
    

    phoneNumber 可能带 (+) 或不带 (+)。

    phoneNumber 看起来像 99455555555 或 +99455555555

    【讨论】:

    • 它可以在没有 + 号的情况下工作,我也用 + 号检查过,它仍然可以工作
    • 不适用于我 +,必须删除它才能让 Whatsapp 真正打开对话,否则我在 Whatsapp 应用程序上收到错误消息“无法打开此链接。检查链接再试一次”
    【解决方案4】:

    适用于 Swift 4.2+ 和 iOS 9+

    方法 1:(如果已安装 WhatsApp 应用程序,则启动)

    let phoneNumber =  "+989160000000" // you need to change this number
    let appURL = URL(string: "https://api.whatsapp.com/send?phone=\(phoneNumber)")!
    if UIApplication.shared.canOpenURL(appURL) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
        }
        else {
            UIApplication.shared.openURL(appURL)
        }
    }
    

    方法二:(使用safari打开WhatsApp短链接网页)

    let phoneNumber =  "+989160000000" // you need to change this number
    let appURL = URL(string: "https://wa.me/\(phoneNumber)")!
    if UIApplication.shared.canOpenURL(appURL) {
        if #available(iOS 10.0, *) {
            UIApplication.shared.open(appURL, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(appURL)
        }
    }
    

    注意:电话号码中的“+”是可以的。

    【讨论】:

    • 完美解决方案
    • 我认为它可以在 iPhone 中打开,但 iPad 呢?有什么解决办法
    • 电话号码不能使用'+'
    • 这是直接打开 WhatsApp 还是先打开 safari?可以同时输入电话号码和短信吗?
    • 工作正常,但如果我想发送图像怎么办!如何将图片添加到网址?
    【解决方案5】:

    Devs Here is my Code for Opening WhatsApp Chat in Xcode 13.0 and iOS 15.0 for specific Contact.

    func navigateToWhatsApp() {
        var countryCode = "91". //Country code
        var mobileNumber = "1234567890" //Mobile number
        let urlString = "https://api.whatsapp.com/send?phone=\(countryCode)\(mobileNumber)"
    
        let urlStringEncoded = urlString.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
    
        let URL = NSURL(string: urlStringEncoded!)
    
        if UIApplication.shared.canOpenURL(URL! as URL) {
            debugPrint("opening Whatsapp")
            UIApplication.shared.open(URL as! URL, options: [:]) { status in
                debugPrint("Opened WhatsApp Chat")
            }
        } else {
            debugPrint("Can't open")
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2020-12-14
      • 2015-06-23
      • 1970-01-01
      • 2017-06-19
      相关资源
      最近更新 更多