我们非常感谢在以下方面提供任何帮助。通过我们的应用程序,用户可以启动一条WhatsApp消息(所发生的情况是,WhatsApp客户端从预先加载的电话+文本开始,因此用户只需要点击WhatsApp应用程序中的“发送”按钮)。
我们有一个机器人和一个iOS应用程序。在安卓系统中,我们使用以下代码在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上实现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业务)将要推出?
谢谢
发布于 2020-10-08 19:04:25
我用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业务应该使用“”。
在此之后,您可以为每个应用程序调用URL,我的意思是,对于WhatsApp,您可以使用这种格式的URL:
let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"对于WhatsApp业务,您必须使用以下格式:
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业务中为您提供发送消息的选项。
但如果您使用其他代码:
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业务之间进行选择的方法是WhatsApp格式。如果您选择这种格式,您将被要求在一个或另一个WA版本中进行选择:
let whatsApp = "https://wa.me/\(phoneNumber)/?text=\(shareableMessageText)"但是,如果您使用此URL格式,您将直接使用WA Business:
let whatsApp = "whatsapp://send?phone=\(phoneNumber)&text=\(shareableMessageText)"https://stackoverflow.com/questions/61877457
复制相似问题