I am using the below code to dial a phone number that I get from contacts:
func dialNumber(number : String) {
let formatedNumber = number.components(separatedBy: NSCharacterSet.decimalDigits.inverted).joined(separator: "")
print("formatedNumber:", formatedNumber)
if let url = URL(string: "tel://\(formatedNumber)"),
UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url, options: [:], completionHandler:nil)
} else {
UIApplication.shared.openURL(url)
}
}
}
Additional info:
var urlTest = URL(string: "tel:+12345678900")!
print("UIApplication.shared.canOpenURL(url):", UIApplication.shared.canOpenURL(urlTest)) // Returns true
UIApplication.shared.open(urlTest, options: [:], completionHandler:nil)
var urlTest = URL(string: "tel:2345678900")!
print("UIApplication.shared.canOpenURL(url):", UIApplication.shared.canOpenURL(urlTest)) // Returns true
UIApplication.shared.open(urlTest, options: [:], completionHandler:nil)
The code works for numbers with Area code +1XXXYYYZZZZ and the phone number is dialed.
But it doesn't work for numbers like XXXYYYZZZZ.
If I open the contact using Address Book, the number XXXYYYZZZZ is recognized as a phone number. On clicking, the number gets dialed.
How can I get a similar behaviour as Address Book inside my app?
I do not want to add a region code +1 as the app will be used in other countries as well.
There are external libraries for identifying phone numbers but I'm trying to explore if there is a native solution.
If a number is not recognized, I even tried to open the contact in the address book so the user can dial the number from the contacts app, but it looks like address book is not a supported scheme.
Related
// 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?
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)"
I have a problem. I am trying to create call by phone number function. But I need to have a phone number in the format (###)###-#### at standard iOS alert. And for some reason, this function gives different results on different screens. How to change this?
if let phoneCallURL:URL = URL(string: "tel://\(phoneNumber)") {
let application:UIApplication = UIApplication.shared
if (application.canOpenURL(phoneCallURL)) {
if #available(iOS 10, *) {
application.open(phoneCallURL, options: [:], completionHandler: {success in
print("Open tel: \(success)")
})
} else {
let success = UIApplication.shared.openURL(phoneCallURL)
print("Open: \(success)")
}
}
}
Page 1:
Page 2:
Update:
I tried use formatted strings, but at first page I get unformatted phone number, and at second page I get phone number with this format: #(##) ### ## ##. Always. No matter I use formatted or unformatted phone number.
It seems to me what must be some kind of extension for managing the telephone label in the system alert.
breakpointing inside the callback and logging success shows false.
I added itms-apps to my plist: LSApplicationQueriesSchemes
and put this in my app delegate:
func application(_ app: UIApplication, open url: URL, options: [UIApplicationOpenURLOptionsKey : Any] = [:]) -> Bool {
return true
}
func moreTapped() {
let url = NSURL(string: "itms-apps://itunes.com/developer/quantum-productions/id979315877")
if #available(iOS 10.0, *) {
UIApplication.shared.open(url! as URL, options: [:], completionHandler: {(success: Bool) in
})
} else {
UIApplication.shared.openURL(url! as URL)
}
}
You have the incorrect format for your URL, and you should use URL rather than NSURL in Swift.
This will open your developer page in the App Store app:
if let url = URL(string:"itms-apps://geo.itunes.apple.com/developer/quantum-productions/id979315877&mt=8") {
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
There is no need to call canOpenURL for standard URL types such as http/s and itms.
Apple just announced the appstore.com URLs.
**
https://developer.apple.com/library/ios/qa/qa1633/_index.html
**
To create an App Store Short Link, apply the following rules to your company or app name:
Remove all whitespace
Convert all characters to lower-case
Remove all copyright (©), trademark (™) and registered mark (®)
symbols
Replace ampersands ("&") with "and"
Remove most punctuation (See Listing 2 for the set)
Replace accented and other "decorated" characters (ü, å, etc.) with
their elemental character (u, a, etc.)
Leave all other characters as-is.
Listing 2 Punctuation characters that must be removed.
!¡"#$%'()*+,-./:;<=>¿?#[]^_`{|}~
Below are some examples to demonstrate the conversion that takes
place.
App Name examples
Ocarina => http://appstore.com/ocarina
According to Apple's latest document items-apps: or items: won't not work. You need to use
appStoreLink =
"https://itunes.apple.com/us/app/apple-store/id375380948?mt=8"
or
> SKStoreProductViewController
did anyone successfully deep linked from his app to a directions to a place in moovit ? why can't i ?! it just opens the app and does nothing ....
if anyone successfully deep linked to a direction to anywhere please help.
if UIApplication.shared.canOpenURL(URL(string: "moovit://")!) {
// Moovit installed - launch app (with parameters)
let MoovitURL: String = "moovit://directions?dest_lat=40.758896&dest_lon=-73.985130&dest_name=TimesSquare&orig_lat=40.735845&orig_lon=-73.990512&orig_name=UnionSquare&auto_run=true&partner_id=<testApp2345>"
let escapedString = MoovitURL.addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
UIApplication.shared.openURL(URL(string: escapedString!)!)
}else {
// Moovit not installed - send to store
UIApplication.shared.openURL(URL(string: "https://itunes.apple.com/us/app/id498477945")!)
}
}
even Moovit's own example isn't working for me....Whats wrong?
The problem is with the .urlHostAllowed parameter.
Using the .urlQueryAllowed parameter instead will only convert the parameters after '?'.
Fixed your code:
if UIApplication.shared.canOpenURL(URL(string: "moovit://")!) {
// Moovit installed - launch app (with parameters)
let MoovitURL: String = "moovit://directions?dest_lat=40.758896&dest_lon=-73.985130&dest_name=TimesSquare&orig_lat=40.735845&orig_lon=-73.990512&orig_name=UnionSquare&auto_run=true&partner_id=<testApp2345>"
let escapedString = MoovitURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
UIApplication.shared.open(URL(string: escapedString!)!, options: [:], completionHandler: nil)
}else {
// Moovit not installed - send to store
UIApplication.shared.open(URL(string: "https://itunes.apple.com/us/app/id498477945")!, options: [:], completionHandler: nil)
}
Try opening this url in Safari and see if it works (It does for me).
Looks like the problem is with the code, maybe try without addingPercentEncoding or use the new openURL API