WhatsApp VS WhatsApp Business in SWIFT

Viewed 1362

We would really appreciate any help on the following. Through our app, the user can initiate a WhatsApp message (what happens is that the WhatsApp client starts with the phone + text preloaded, so the user just needs to tap the "send" button from the WhatsApp application).

We have an Android and an iOS app. In Android we are using the following code to select between WhatsApp and 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();
  }

We are trying to achieve the same functionality on Swift for iOS, however, we did not find any way to programmatically define whether the OS should start WhatsApp or WhatsApp Business. The code listed below, always starts the one or other depending on which is installed. If both are installed, it starts the WhatsApp application.

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

So in simple words, is there any way, from our app, to select which WhatsApp application (WhatsApp or WhatsApp Business) is going to be launched?

Thanks

1 Answers

I have made some apps with WhatsApp but I had to use the web platform, not the business app.

You can check what app is installed in the device like this:

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

The 'App1' value must be changed for the app you want to check. WhatsApp App should use 'WhatsApp', and WhatsApp Business should use 'WhatsApp-Business'.

After that you can call the URL for each app, I mean, for WhatsApp you can use the URL with this format:

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

And for WhatsApp Business you have to use this format:

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

It is possible too, that the first step was not necessary to do. Because of the call is made with the api, the device should open the Business app, and if it is made with the wa.me scheme, the device should open the WhatsApp as normal.

I am going to check my app to see if it is working or not.

UPDATE:

I have installed WhatsApp Business and I have made some test, with two different url calls.

The code I use is this:

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

Using this code you will see a prompt with a message giving you the option of send a message in WhatsApp or WhatsApp Business.

But if you use this other code:

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

You will see a prompt asking you to open WhatsApp business. So the way to choose between WhatsApp and WhatsApp Business is the URL format. If you choose this format you will be ask to choose between one or another WA version:

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

But if you use this URL format, you will use WA Business directly:

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