I am happy to see you here! In this tutorial, we will explore two very important functionalities in Xamarin: how to manage the Phone Dialer and how to send SMS. We will learn how to use them via the APIs that Xamarin.Essentials provides us. š
Nowadays, it is useful to integrate phone calls and SMS interactions within our applications to make a product that covers at least the basic functionalities of the user. This way, users can remain in the applications without the need to exit an app in order to send a message or make a phone call.
In the past, we previously had to add two NuGet packages ā one for calls and one for SMS ā which was complicated and time-consuming. Now, we can create a phone dialer in a much easier way. Letās start by explaining how to use the phone dialer, and then SMS.
Xamarin.Essentials provides a cross-platform developer API for your mobile applications and allows us to develop in the entire Xamarin Forms environment, Android, iOS, or UWP.
Great! So what functionalities/APIs does Xamarin.Essentials offer? There are several, such as SMS, accelerometer, phone dialer, preferences, and many others. If you want to learn about all of them and how to implement each one, I recommend this article, where you will have the feature guide to each functionality.
Itās important to know that Xamarin.Essentials supports a minimum version of Android 4.4, corresponding to API level 19, but the target Android version for build must be 9.0 or 10.0, corresponding to API level 28 and level 29.
If you are up to date, Xamarin.Essentials is already added to your projects, so you donāt need an additional configuration at this time.
If you have an older version and Xamarin.Essentials isnāt working well, you can check to see if everything is correct with your implementation in the docs.
In some cases, each platform needs an additional setup to make the implementation effective. In this case, the configuration is only needed for Android. Letās set up all the configurations needed to use the phone dialer and SMS.
First, open the MainActivity.xml file from your Android project, and, inside of the manifest node, add the following code:
š Phone dialer:
<queries> <intent> <action android:name="android.intent.action.DIAL" /> <data android:scheme="tel"/> </intent> </queries>
š„ SMS:
<queries> <intent> <action android:name="android.intent.action.VIEW" /> <data android:scheme="smsto"/> </intent> </queries>
Itās important to verify your target Android version. To do so, just follow the next steps:
As the image shows above, please apply the following steps:
If your target Android version is set to Android 11, you must update your Android manifest with queries that are used with the new package visibility requirements.
Both iOS and UWP have no additional setup required.
To make phone calls, we have a PhoneDialer
class, which allows us to open the phone number dialer.
Internally, when we are using this API, it formats the telephone number based on its country of origin for when it shows up on the telephone keypad.
Letās see the structure to implement:
First, we create the method for calls that can receive the telephone number parameter and has the name PlacePhoneCall
. Next, we add the most important class of this explanation, which is PhoneDialer
. This class allows us to open the numeric keyboard of our phones.
Finally, with the Open
method, we add the telephone number that we want to be displayed when the numeric keyboard is opened. (We already asked for this information in the parent method called PlacePhoneCall
.)
Hereās the code example:
public void PlacePhoneCall(string number) { PhoneDialer.Open(number); }
Done! Our phone dialer is ready! š
The SMS
class allows us to open the message board through the ComposeAsync
method, which receives a SmsMessage
value as a parameter.
The SMS message receives the body and the recipient(s) as values, like so:
Now letās see the graphical structure:
In this example, we create the SendSms
method in which we can add the class to send the SMS. We will receive the parameters of the SMS text and the recipients.
Within the previously created method, we added the Sms
class, which is in charge of opening the message board with the desired text.
Finally, we add the ComposeAsync
method, which will receive the message that you want to present. Note that we are using an asynchronous method. If you want to know more information on this topic, check out this article.
ā Note that the message variable is type
SmsMessage
, which is responsible for receiving the text of the message. Later, the recipients will be sent in theComposeAsync
method. You will see this example in the code implementation added below.
Hereās a code example:
public async Task SendSms(string messageText, string recipient) { var message = new SmsMessage(messageText, new []{ recipient }); await Sms.ComposeAsync(message); }
If you want to send SMS to more than one user, you can! To do so, you must change the parameter type by a string array:
public async Task SendSms(string messageText, string[] recipients) { var message = new SmsMessage(messageText, recipient ); await Sms.ComposeAsync(message); }
Done! Our SMS is ready! š Thanks for reading!
Install LogRocket via npm or script tag. LogRocket.init()
must be called client-side, not
server-side
$ npm i --save logrocket // Code: import LogRocket from 'logrocket'; LogRocket.init('app/id');
// Add to your HTML: <script src="https://cdn.lr-ingest.com/LogRocket.min.js"></script> <script>window.LogRocket && window.LogRocket.init('app/id');</script>
Vite is a versatile, fast, lightweight build tool with an exceptional DX. Let’s explore when and why you should adopt Vite in your projects.
Explore advanced capabilities for content sharing with the navigator.share
API, including dynamic content sharing, custom share targets, and batch sharing.
We spoke with Chas to get his insights on building technology and internal processes for companies that are scaling quickly.
Cypress is one of today’s foremost tools for testing web applications. Let’s explore when and why you should adopt Cypress in your projects.