In continuation to blog posts in this series on plugins, today we’ll cover a plugin/library to perform FileIO.
There are different APIs on different platforms to perform FileIO. Having said that, Xamarin already makes life easier by implementing System.IO which is available in Android and iOS.
Following code can be used in Android to create a file and then read contents from it.
Similarly, following code can be used in case of iOS application.
In case of Windows, using WinRT APIs, the case is little bit different as shown below:
Now to make life easier, there is a NuGet from Daniel Plaisted, which can help you to use same code on different platforms. You can find more information about the library from here: https://www.nuget.org/packages/PCLStorage/
This way, you can use one code (in C#) across all the three different platforms.
Hope you’re enjoying this series. Do let me know your feedback if you want to include any specific libraries/features.
In continuation to blog posts in this series on plugins, today we’ll cover a plugin to make phone calls.
As we understood before, all platforms support phone calling (just like emailing, texting, etc…) and Xamarin makes it easier with one common language – C#. However, developers still need to learn
In case of Windows Phone, one can initiate a phone call by using following code. In this case, Windows Phone has special class which allow developers to perform this task.
In case of Android, developers can use ‘Intent’ and ‘URI activation’ to start a new phone call.
In case of iOS, same concept of URI activation can be applied. As shown below in the code, developers can create a URI with telephone number and it will launch default application to make a phone call.
Now, in case of Xamarin.Forms developers can use same Messaging Plugin for Xamarin and Windows plugin to implement phone functionality across the platforms.
This way, developers can utilize the power of plugins to write one code which can work across different platforms, without bothering about underlying APIs.
Xamarin provides native, cross-platform mobile application development using language we all know and love – C#. However, it is still required to learn concepts of each platform. For example to send text message (SMS) in all these three platforms there are different set of APIs.
Below API is for Android, which uses SmsManager to send the text message. There is another way to do this using Intents.
In case of Windows Phone, ChatMessage allows us to declare and send text message. This API is available in WinRT platform. In case of SL based apps, we need to use Launchers/Choosers.
In case of iOS as shown below, there is no API as such. But we use URL to launch default messaging app. In this case, we cannot set the message body, which we want to send out.
Life will be much simpler if there will be just one API on all these platforms which will take care of sending text messages and as I developer we don’t need to bother about on which platform it is being called. Here comes components and plugins. My friend James has written a nice blog about ‘What Exactly is a Plugin for Xamarin?’. You can read it here
In our case, we’re going to use Messaging Plugin for Xamarin and Windows which allow us to use same API to send text messages. Below code is written once in a Xamarin.Forms project which depending on platform on which code is executing, send the message using that platform’s APIs.
Using this plugin, it becomes easier to send text messages on Android, iOS and Windows. And this is the magic of Plugins for Xamarin.
In future posts, we’ll cover some of the best plugins and components which can help you build cross-platform mobile applications with single code-base.
I hope you’ve enjoyed this.