gpt4 book ai didi

ios - SWIFT 中的 WhatsApp VS WhatsApp Business

转载 作者:行者123 更新时间:2023-12-05 06:16:57 28 4
gpt4 key购买 nike

如果您能在以下方面提供帮助,我们将不胜感激。通过我们的应用程序,用户可以启动 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 制作了一些应用程序,但我必须使用网络平台,而不是商业应用程序。

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

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

关于ios - SWIFT 中的 WhatsApp VS WhatsApp Business,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61877457/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com