Formatted phone number in Call Alert. Swift - ios

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.

Related

How to change the phone number format of call permission popup Swift

I am making a call to a number and it is working fine. Is there a way to change the number format of the permission asking popup in swift ?
Here is my code:
if let url = URL(string: "tel://\("
8680047015 ")"),
UIApplication.shared.canOpenURL(url) {
if #available(iOS 10, *) {
UIApplication.shared.open(url)
} else {
UIApplication.shared.openURL(url)
}
}
As of now the popup is as follows. I would like to change it to 868-004-7015..
While trying a US number, it is showing as 1234 - 567890. How formatting is happening?
Any help is really appreciated.

How can I send message to specific contact through WhatsApp from my ios app using swift?

// 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?

Open url tel scheme not working for some phone numbers

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.

WhatsApp VS WhatsApp Business in SWIFT

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

How to get the card number from braintree payments?

I am using braintree pod for my project and it is working nicely after getting nonce. But I want to retrieve the card icon and digits of the card number.
How can I get both card icon in an imageView and card number in UILabel?
I used two pod files.
pod 'BraintreeDropIn'
pod 'BraintreeDropIn/UIKit'
and I am using this function provided from braintree documentation.
func showDropIn(clientTokenOrTokenizationKey: String) {
let request = BTDropInRequest()
//BTUIKAppearance.darkTheme()
//BTUIKAppearance.sharedInstance().primaryTextColor = UIColor.green
let dropIn = BTDropInController(authorization: clientTokenOrTokenizationKey, request: request)
{ (controller, result, error) in
if (error != nil) {
print("ERROR : \(error.debugDescription)")
} else if (result?.isCancelled == true) {
print("CANCELLED")
} else if let result = result {
// Use the BTDropInResult properties to update your UI
//result.paymentOptionType
//result.paymentMethod
//result.paymentIcon
//result.paymentDescription
if let outPut = result.paymentMethod {
print("\nNonce : \(outPut.nonce)")
self.paymentNonce = outPut.nonce
self.paymentCardType = result.paymentDescription.description
}
}
controller.dismiss(animated: true, completion: nil)
}
self.present(dropIn!, animated: true, completion: nil)
}
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
Right now, Braintree's iOS SDK does not return the card brand icon in the result. However, an image URL is returned in a transaction response object that contains the card brand icon within the imageUrl attribute. You can check out this gist on how to display image URLs in Swift. Alternatively, the type is available on the client when the nonce is returned.
For PCI compliance reasons, the full card number is not ever exposed to a merchant if you are using the Drop-in UI. Instead, the iOS SDK will provide the lastTwo digits if you cast the payment method to BTCardNonce. Swift's documentation has a lot of good info on casting. Though Objective-C, an example would be:
BTCardNonce *cardNonce = (BTCardNonce*)result.paymentMethod;
Alternatively, you can get a maskedNumber from the transaction response object, similarly to the image URL.
Edited: updated link for adding image URL to Swift 4

Resources