Introduction
Being a web developer entices me knowing how I can use my web skills in Xamarin as well. With the quest to do this, I found Webview. So in short, it is a browser which can display HTML within your App. Well, it can also run your JavaScript and style your HTML using CSS.
Background
What I had to achieve here was to fire up Phone dialer when I click on the link which contains tel:
. This is something similar to mailto:
which will trigger your email client to open up and you can write email to the address specified.
Using the Code
Well just adding the link doesn't work in the Webview. So we need to create an Intent to start the Phone dialer. The solution below has been implemented in Android for now and not on iOS. Will put an addendum once done.
So to do this, we use Dependency Injection like this:
- Create an Interface in your PCL:
public interface IAppIntents
{
void HandleWebviewUri(string uri);
}
- Now create a class in your Android project to implement the above
interface
:
public class AppIntents_Droid : IAppIntents
{
public void HandleWebviewUri(string uri)
{
var appUri = Android.Net.Uri.Parse(uri);
var appIntent = new Intent(Intent.ActionView, appUri);
Application.Context.StartActivity(appIntent);
}
}
- Whenever user clicks on a link in Webview, a
Navigating
event is fired which we need to capture and call the above method:
void WvMain_Navigating(object sender, WebNavigatingEventArgs e)
{
If(e.Url.Contains("tel:")
{
DependencyService.Get<iappintents>().HandleWebviewUri("tel:+9809801234");
}
}
Pretty simple right!
Points of Interest
Xamarin is a wonderful framework to work with if you have a craving to develop Web apps. With Webview, you can extend your existing Web developer skills to develop great mobile apps. Do let me know if you implement it in your project and if you find this article helpful.