Ios WhatsApp与SWIFT中的WhatsApp业务

Ios WhatsApp与SWIFT中的WhatsApp业务,ios,swift,whatsapp,Ios,Swift,Whatsapp,我们非常感谢您在以下方面的帮助。通过我们的应用程序,用户可以启动WhatsApp消息(发生的情况是WhatsApp客户端在手机+文本预加载的情况下启动,因此用户只需点击WhatsApp应用程序中的“发送”按钮) 我们有一个Android和一个iOS应用程序。在Android中,我们使用以下代码在WhatsApp和WhatsApp业务之间进行选择 String url = "https://api.whatsapp.com/send?phone=" + phoneNumberToUse + "

我们非常感谢您在以下方面的帮助。通过我们的应用程序,用户可以启动WhatsApp消息(发生的情况是WhatsApp客户端在手机+文本预加载的情况下启动,因此用户只需点击WhatsApp应用程序中的“发送”按钮)

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

  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业务。下面列出的代码始终启动一个或另一个,具体取决于安装的代码。如果两者都已安装,则会启动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业务)


谢谢

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

您可以通过以下方式检查设备中安装的应用程序:

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应用程序应使用“WhatsApp”,WhatsApp业务应使用“WhatsApp业务”

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

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
let whatsApp = "https://api.whatsapp.com/send?phone=\(phoneNumber)&text=\(shareableMessageText)"
对于WhatsApp业务,您必须使用以下格式:

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
let whatsApp = "https://api.whatsapp.com/send?phone=\(phoneNumber)&text=\(shareableMessageText)"
也有可能第一步是不必要的。由于调用是使用api进行的,因此设备应打开业务应用程序,如果使用wa.me方案进行调用,则设备应正常打开WhatsApp

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

更新:

我已经安装了WhatsApp业务,并通过两个不同的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业务之间进行选择的方法是URL格式。如果您选择此格式,将要求您在一个或另一个WA版本之间进行选择:

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
但如果您使用此URL格式,您将直接使用WA Business:

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

我用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应用程序应使用“WhatsApp”,WhatsApp业务应使用“WhatsApp业务”

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

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
let whatsApp = "https://api.whatsapp.com/send?phone=\(phoneNumber)&text=\(shareableMessageText)"
对于WhatsApp业务,您必须使用以下格式:

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
let whatsApp = "https://api.whatsapp.com/send?phone=\(phoneNumber)&text=\(shareableMessageText)"
也有可能第一步是不必要的。由于调用是使用api进行的,因此设备应打开业务应用程序,如果使用wa.me方案进行调用,则设备应正常打开WhatsApp

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

更新:

我已经安装了WhatsApp业务,并通过两个不同的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业务之间进行选择的方法是URL格式。如果您选择此格式,将要求您在一个或另一个WA版本之间进行选择:

let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"
但如果您使用此URL格式,您将直接使用WA Business:

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

您是否尝试过联系whatsapp技术支持?他们可能会告诉您是否为whats应用程序和业务应用程序指定了单独的LSApplicationQueriesScheme您是否尝试过联系whatsapp技术支持?他们可能会告诉您是否为whats应用程序和业务应用程序指定了单独的LSApplicationQueriesScheme