简体   繁体   English

如何从 Swift 应用程序打开 WhatsApp?

[英]How to open WhatsApp from Swift app?

I am using webview for my Swift app and I have "Share on WhatsApp" button on my website which works fine on a browser.我正在为我的 Swift 应用程序使用webview ,我的网站上有“在 WhatsApp 上分享”按钮,它在浏览器上运行良好。 But on iPhone app, when I click on the button, nothing happens.但是在 iPhone 应用程序上,当我点击按钮时,什么也没有发生。

How to open WhatsApp from my app?如何从我的应用程序打开 WhatsApp? I am using Xcode 8 and iOS 10.我正在使用 Xcode 8 和 iOS 10。

I know this is an old question, but the following worked for me (I'm using xcode 8.3.3 and swift 3).我知道这是一个老问题,但以下对我有用(我使用的是 xcode 8.3.3 和 swift 3)。

I added whatsapp query scheme inside Info.plist.我在 Info.plist 中添加了 whatsapp 查询方案。

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

Info.plist信息表

After adding it, the following works:添加后,效果如下:

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

For Swift 4.2+ and iOS 9+适用于 Swift 4.2+ 和 iOS 9+

Method 1: (launches WhatsApp application if is installed)方法 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)
    }
} else {
    // WhatsApp is not installed
}

Method 2:(open WhatsApp short-link web page using safari)方法二:(使用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)
    }
}

Method 3: if WhatsApp is installed launch app, otherwise open short-link in safari.(combine method 1 and 2)方法三:如果安装了WhatsApp启动应用程序,否则在safari中打开短链接。(结合方法一和方法二)

Note : '+' in phone number is OK.注意:电话号码中的“+”是可以的。

UIApplication.shared.openURL(URL(string:"https://api.whatsapp.com/send?phone=phoneNumber")!)

phoneNumber might be with (+) or not.电话号码可能带 (+) 或不带。

phoneNumber looks like 99455555555 or +99455555555电话号码看起来像 99455555555 或 +99455555555

For this you should use URL schemes.为此,您应该使用 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
        }
    }
} 

Here is a simple answer I use in a switch structure.这是我在开关结构中使用的一个简单答案。 This loads whatsApp.这将加载 WhatsApp。 I am still looking for a way to call a specific contact.我仍在寻找一种呼叫特定联系人的方法。

case "whatsApp":
        let usefullWhere: String = "whatsapp://?app"//
        let url = NSURL(string: usefullWhere)!
        UIApplication.sharedApplication().openURL(url)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM