Xamarin.Essentials — Access cross-platform APIs easily from your Xamarin.Forms app
If you start creating a mobile app these days, one of your goal will be that the app is runnable and available many different platforms like Android, iOS or even UWP. In this case I will prefer the usage of Xamarin.Forms, because the approach of Xamarin.Forms is to minimize platform specific code and write your business logic and UI layer only once.
Sometimes this isn’t easy for example if you need to know the current position of the user. Here are some limitations because each platform has its own approach and its own API. You must also set the permissions to access the information on each platform. This is only one example, but there are many more like reading/writing a file from/to the local file system or send a SMS and so much more.
Let’s talk again about getting the current position of the user. In this case you normally need to write code for each platform and you introduce an interface to be able to perform the action within the portable part of your app. But luckily with the NuGet package called Xamarin.Essentials you have access to many platform specific APIs directly in your platform-independent code. Because the developers of this package have written a platform specific implementation and also provide the interface so you are able to use them directly in your business logic.
Currently, a large number of APIs are already supported and with each version new features or even new APIs are added. The following list shows the currently available APIs with a short description.
- Accelerometer — Retrieve acceleration data of the device
- App Information — Find out information about the application
- Battery — Easily detect battery level, source, and state
- Clipboard — Quickly and easily set or read text on the clipboard
- Compass — Monitor compass for changes
- Connectivity — Check connectivity state and detect changes
- Data Transfer — Send text and website uris to other apps
- Device Display Information — Get the device’s screen metrics
- Device Information — Find out information about the device
- Email — Easily send email messages (also with attachment)
- File System Helpers — Easily save/read files to/from app data
- Flashlight — A way to turn the flashlight on/off
- Geocoding — Geocode and reverse geocode addresses and coordinates
- Geolocation — Retrieve the device’s GPS location
- Gyroscope — Track rotation around of the device
- Magnetometer — Detect device’s orientation
- MainThread — Run code on the application’s main thread
- Open Browser — Quickly open a browser to a specific website.
- Orientation Sensor — Retrieve the orientation of the device
- Phone Dialer — Open the phone dialer
- Power — Obtain the device’s energy-saver status
- Preferences — Quickly and easily add persistent preferences
- Screen Lock — Keep the device screen awake
- Secure Storage — Securely store data
- SMS — Create an SMS message for sending it
- Text-to-Speech — Vocalize text on the device
- Version Tracking — Track the applications version and build numbers
- Vibrate — Make the device vibrate
The package contains APIs for UWP, Android and iOS.
On Android we need to setup the usage of Xamarin.Essentials. In the class
MainActivity within the
OnCreate method we need to initialize the package
To handle the permissions we had to override the method
OnRequestPermissionsResult in the same file as shown in the following snippet.
To use the Xamarin.Essentials package on UWP or iOS you don’t need to setup anything. Just add the package to the project a start using it.
Example: Get last known location
For example, to query the last known position of the device, only these lines of code in the portable part of the Xamarin.Forms app are needed:
As you can see in this code snippet we call the method
GetLastKnownLocationAsync from the static class
Geolocation in the namespace
Xamarin.Essentials to get the last known position of the device. The result is an object which contains values for latitude and longitude. In this case we simply show the values in a dialog.
The following animation shows a section of the demo application on Android:
And here the same app under UWP:
Normally you need to write platform specific code within your cross-platform application, but with the NuGet package Xamarin.Essentials you have easily access to many APIs which are very useful in your Xamarin.Forms app.
So please be sure to take a look at it. If you want to start using Xamarin.Essentials today feel free to grab the NuGet package and try it out yourself. You can find the full documentation on docs.microsoft.com.
And did I mentioned that this an open-source project? So you are able to help the developers with your own suggestions or by creating issues if something doesn’t work as expected.