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
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
This question already has answers here:
Share PDF through WhatsApp
(2 answers)
Closed 1 year ago.
I am using below code to send a text to a new WhatsApp number using a WhatsApp Url scheme instead of UIActivityViewController.
let urlWhats = "whatsapp://send?phone=+9197******2&text=Hi Hello"
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters:
CharacterSet.urlQueryAllowed){
if let whatsappURL = URL(string: urlString) {
if UIApplication.shared.canOpenURL(whatsappURL){
if #available(iOS 10.0, *) {
UIApplication.shared.open(whatsappURL, options: [:], completionHandler: nil)
} else {
UIApplication.shared.openURL(whatsappURL)
}
} else {
print("Install Whatsapp")
}
My current requirement is to share a Pdf file to a new WhatsApp number using WhatsApp Url scheme. Can anyone help me with this..
You can not pass the pdf file via url, it's make nonsense.
There are another way you can do is custom the UIActivityViewController.
You can refer this tutorial for UIActivityViewController customization: https://nshipster.com/uiactivityviewcontroller/
// 1
let urlWhats = "https://wa.me/\(mobile)/?text=\(text)"
// 2
if let urlString = urlWhats.addingPercentEncoding(withAllowedCharacters: NSCharacterSet.urlQueryAllowed) {
// 3
if let whatsappURL = NSURL(string: urlString) {
// 4
if UIApplication.shared.canOpenURL(whatsappURL as URL) {
// 5
UIApplication.shared.open(whatsappURL as URL, options: [:], completionHandler: nil)
// UIApplication.shared.\
} else {
// 6
print("Cannot Open Whatsapp")
}
}
}
I'm able to launch whatsapp from my app from the above mentioned code, it is composing prefix text to the contact I wish to send and I need to click the send button in whatsapp manually . But I'm looking for a code which automatically sends whatsapp text to number from my app. Can anyone share your thoughts on this?
You can only compose the message for a particular contact using the Deep Linking method that you have used for it. For sending the message user has to click on the send button manually. You could provide the user with an alert that says so. But, it's not possible to do it for the user from your side. If you were able to send a message on Whatsapp by writing code without the user's confirmation it would be a break of user's privacy. Don't you think?
I am using universal link in iOS application. For the universal link I am using of Firebase dynamic link.
Universal link is working fine when app is installed. But when app is not installed then this link is opening in safari.
I am creating dynamic link (Universal link) programatically in Swift.
func createDynamicLinkForConsumer(){
guard let link = URL(string: "https://<Domain-Name>?consumerID=\(Auth.auth().currentUser?.uid ?? "")&type=consumerReferral") else { return }
let dynamicLinksDomainURIPrefix = "<appName>.page.link"
let linkBuilder = DynamicLinkComponents(link: link, domain: dynamicLinksDomainURIPrefix)
linkBuilder.options?.pathLength = .short
linkBuilder.iOSParameters = DynamicLinkIOSParameters(bundleID: "<Bundle-ID>")
linkBuilder.iOSParameters?.appStoreID = "<app-store-id>"
linkBuilder.androidParameters = DynamicLinkAndroidParameters(packageName: "<android package name>")
linkBuilder.navigationInfoParameters?.isForcedRedirectEnabled = true
guard let longDynamicLink = linkBuilder.url else { return }
print("The long URL is: \(longDynamicLink)")
self.referralLinkLabel.text = String(describing: longDynamicLink)
DynamicLinkComponents.shortenURL(longDynamicLink, options: nil) { (url, warnings, error) in
if url != nil {
print("Short URL is: \(url)")
self.referralLinkLabel.text = String(describing: url!)
}
}
}
This is the screen which opens after when safari opens the link:
After tapping on "Open" button of alert box browser opens app store app.
Actually what I would like to do is to redirect and go to the app store, so users can download the app directly. I don't want to this two steps to open app store.
You should first init DynamicLinkNavigationInfoParameters, then set property to true.
linkBuilder.navigationInfoParameters = DynamicLinkNavigationInfoParameters()
linkBuilder.navigationInfoParameters?.isForcedRedirectEnabled = true
Hi you can reduce these steps with this check on the dynamic link
But you should have in mind that you can have a problem with your link in Messanger app and some other apps.
I have an app and I wanna add messages extension feature.
I thought the feature is if the user selects a message, it switches my host app directly like google map.
I made a MSMessage and set URL and the message has template layout which had caption and sub-caption.
let message = MSMessage()
message.url = "http://blahblah?customScheme=myHostAppLaunchScheme"
let template = MSMessageTemplateLayout()
template.image = sampleImage
template.caption = "this is a caption"
template.subCaption = "this is a sub caption"
message.layout = template
guard let conversation = activeConversation else {
print("blahblah")
return
}
conversation.insert(message) { (error) in
print("finish. error = \(error == nil ? "nil" : error!.localizedDescription)")
}
and i wrote a code extensionContext.open(url, completionHandler) in
willBecomeActive(with conversation: MSConversation)
didReceive(_ message: MSMessage, conversation: MSConversation)
of course, i parsed selectedMessage's URL.
but it didn't work I expected.
the messages extension switches expand mode automatically.
it works if I used
conversation.insertText("myHostAppLaunchScheme", nil)
but I don't want it because it can't add template :(
is there any idea to switch iMessage to host app directly?
thanks for any ideas.
i think i found the answer.
there is no way with using
conversation.insert(message, completionHandler)
i think apple music and google maps are using
conversation.insertText("some url", completionHandler)
because i copied an URL after long press a message which is shared by apple music or google map
then i use the URL in my code
conversation.insertText("the URL", completionHandler)
it's working they did!!
So I am about to launch an app to the App Store. My issue is that I have a rate my app please button but I do not know the right code to insert there.
My fiends tried this on their app and said it was no good:
Does anybody know how I can fix this issue?
let iTunesReviewPageLink = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?id=1073785561&pageNumber=0&sortOrdering=2&type=Purple+Software&mt=8"
// Go directly to review page on App Store
if let url = NSURL(string: iTunesReviewPageLink) {
UIApplication.sharedApplication().openURL(url)
}
The only unknown thing is the ID, right? You can see the ID of your app before it is published - once you set it up in iTunes Connect.
If your app is not yet released, you haven't got an App Store link. So that's impossible.
In order to implement this functionality when your app is released, you can use the following code:
Swift 2
let appIDString = "APP_ID" // your app ID
let reviewsURLString = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?pageNumber=0&sortOrdering=1&type=Purple+Software&mt=8&id=\(appIDString)"
let reviewsURL = NSURL(string: reviewsURLString)
if reviewsURL != nil && UIApplication.sharedApplication().canOpenURL(reviewsURL!) {
UIApplication.sharedApplication().openURL(reviewsURL!)
}
else {
// handle situation if reviews url cannot be opened.
}
Swift 3
let appIDString = "APP_ID" // your app ID
let reviewsURLString = "http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?pageNumber=0&sortOrdering=1&type=Purple+Software&mt=8&id=\(appIDString)"
let reviewsURL = URL(string: reviewsURLString)
if reviewsURL != nil && UIApplication.shared.canOpenURL(reviewsURL!) {
UIApplication.shared.openURL(reviewsURL!)
}
else {
// handle situation if reviews url cannot be opened.
}
EDIT:
This links works in iOS 8 and 9 and links directly to reviews page of the app in App Store application.
I am not sure about iOS 7. Probably for iOS 7 you need to use different link.