【问题标题】:WhatsApp VS WhatsApp Business in SWIFTSWIFT 中的 WhatsApp 与 WhatsApp 业务
【发布时间】:2020-05-18 19:25:20
【问题描述】:

我们非常感谢以下方面的任何帮助。通过我们的应用程序,用户可以发起一条 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)?

谢谢

【问题讨论】:

  • 您是否尝试过联系whatsapp 技术支持?他们可能会告诉您,他们是否为 whats 应用程序和业务应用程序指定了单独的 LSApplicationQueriesScheme whatsapp.com/contact/?subject=messenger
  • 嘿,@tsik 你找到解决方案了吗?

标签: ios swift whatsapp


【解决方案1】:

我用 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 app,如果是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)"

【讨论】:

  • @JigarFumakiya 你能把问题解释清楚一点吗? “未按预期工作”不是有用的评论或答案,因此它无助于改进问题的解决方案。
  • 当然 @wazowski 我已经在同一部 iPhone 中安装了 WhatsApp 和 WhatsAppBusiness,并且您的回答建议使用 let whatsApp = "whatsapp://send?phone=(phoneNumber)&text=(shareableMessageText)" 将打开 WB但是在打开 WB 之后,它要求在 WhatsApp 中打开。知道为什么吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-03-16
  • 1970-01-01
  • 2021-01-29
  • 2022-10-20
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多