首页 > 解决方案 > SWIFT 中的 WhatsApp 与 WhatsApp 业务

问题描述

我们非常感谢以下方面的任何帮助。通过我们的应用程序,用户可以发起 WhatsApp 消息(发生的情况是 WhatsApp 客户端以预加载的电话 + 文本开始,因此用户只需从 WhatsApp 应用程序中点击“发送”按钮)。

我们有一个 Android 和一个 iOS 应用程序。在 Android 中,我们使用以下代码在 WhatsApp 和 WhatsApp Business 之间进行选择。

  String url = "https://api.whatsapp.com/send?phone=" + phoneNumberToUse + "&text=" + 
  URLEncoder.encode(messageText, "UTF-8");
  if(useWhatsAppBusiness){
    intent.setPackage("com.whatsapp.w4b");
  } else {
    intent.setPackage("com.whatsapp");
  }
  URLEncoder.encode(messageText, "UTF-8");
  intent.setPackage("com.whatsapp");
  intent.setData(Uri.parse(url));

  if (intent.resolveActivity(packageManager) != null) {
    startActivity(intent);
  } else {
    Toast.makeText(this, "WhatsApp application not found", Toast.LENGTH_SHORT).show();
  }

我们正在尝试在 Swift for iOS 上实现相同的功能,但是,我们没有找到任何方法来以编程方式定义操作系统是否应该启动 WhatsApp 或 WhatsApp Business。下面列出的代码总是根据安装的启动一个或另一个。如果两者都安装了,它会启动 WhatsApp 应用程序。

  let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"

  guard let url = URL(string: whatsApp) else {
      return
  }
  if #available(iOS 10.0, *) {
      UIApplication.shared.open(url, options: convertToUIApplicationOpenExternalURLOptionsKeyDictionary([:]), completionHandler: nil)
  } else {
      UIApplication.shared.openURL(url)
  }

简单来说,有没有办法从我们的应用程序中选择要启动的 WhatsApp 应用程序(WhatsApp 或 WhatsApp Business)?

谢谢

标签: iosswiftwhatsapp

解决方案


我用 WhatsApp 制作了一些应用程序,但我必须使用网络平台,而不是商业应用程序。

您可以像这样检查设备中安装了哪些应用程序:

let app = UIApplication.shared
let appScheme = "App1://app"
if app.canOpenURL(URL(string: appScheme)!) {
    print("App is install and can be opened")
} else {
    print("App in not installed. Go to AppStore")
}

必须为您要检查的应用更改“App1”值。WhatsApp App 应使用“WhatsApp”,而 WhatsApp Business 应使用“WhatsApp-Business”。

之后,您可以调用每个应用程序的 URL,我的意思是,对于 WhatsApp,您可以使用以下格式的 URL:

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"

对于 WhatsApp Business,您必须使用以下格式:

let whatsApp = "https://api.whatsapp.com/send?phone=\(phoneNumber)&text=\(shareableMessageText)"

也有可能不需要第一步。由于是使用api调用,设备应该打开Business应用,如果是使用wa.me方案,设备应该正常打开WhatsApp。

我要检查我的应用程序,看看它是否正常工作。

更新:

我已经安装了 WhatsApp Business 并进行了一些测试,使用了两个不同的 url 调用。

我使用的代码是这样的:

let phoneNumber = "my_phone_number"
let shareableMessageText = "This_is_a_test_message"

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"

if let urlString = whatsApp.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
    if let whatsappURL = NSURL(string: urlString) {
        if UIApplication.shared.canOpenURL(whatsappURL as URL) {
            UIApplication.shared.openURL(whatsappURL as URL)
        } else {
            print("error")
        }
    }
}

使用此代码,您将看到一条消息提示,让您可以选择在 WhatsApp 或 WhatsApp Business 中发送消息。

但是,如果您使用其他代码:

let phoneNumber = "my_phone_number"
let shareableMessageText = "This_is_a_test_message"

let whatsApp = "whatsapp://send?phone=\(phoneNumber)&text=\(shareableMessageText)"

if let urlString = whatsApp.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) {
    if let whatsappURL = NSURL(string: urlString) {
        if UIApplication.shared.canOpenURL(whatsappURL as URL) {
            UIApplication.shared.openURL(whatsappURL as URL)
        } else {
            print("error")
        }
    }
}

您将看到一个提示,要求您打开 WhatsApp 业务。因此,在 WhatsApp 和 WhatsApp Business 之间进行选择的方式是 URL 格式。如果您选择这种格式,您将被要求在一个或另一个 WA 版本之间进行选择:

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"

但是如果你使用这种 URL 格式,你会直接使用 WA Business:

let whatsApp = "whatsapp://send?phone=\(phoneNumber)&text=\(shareableMessageText)"

推荐阅读