.NETAndroidappleC#Xamarin

Xamarin Forms: Open external application

open-app-xamarin-forms-logo

In this blog post, I will show you how to open external application from Xamarin.Forms application.

My demo application will try to open locally installed application, otherwise it will open app store web page where app can be downloaded and installed.

So, let’s start.

First, I create Xamarin Forms application targeting Android and iOS. In Visual studio this is trivial: New project-> Xamarin Forms -> and then select one of proposed templates.

Application launching procedure is different between platforms. Therefore, I will implement platform specific service for this task. First, I need definition of my service in my common project:

Let’s start with platform specific implementation of this service interface.

Android implementation

I want go into details, but I think the code below is straightforward and enough self-descriptive:

Ok, I have Android launcher code ready. Now, I need to register this service in my Dependency Container to be accessible also on my shared code where my main logic is. You can find service registration in line 27 in code below.

On Android, there is no any special permissions, just android.permission.ACCESS_NETWORK_STATE in AndroidManifest.xml so the app can navigate to Google play web app if app is not installed.

That’s all to it, lets switch to iOS.

iOS implementation

Again, no much to debate, simple iOS service implementation and DI registration.

iOS has more strict policy regarding opening external apps. Both target and source (calling) apps must have defined app schema in order to be able to start target application.

In my case, my demo app was planning to open Netflix application (se code below), this app defines nflx:// app query schema. Likewise, my iOS calling application needs application query schema definition in Info.plist. Here is the extract:

Finally, iOS and Android launcher service implementations are ready and registered in DI Container. I will switch to common code where all my logic is.

Common code

I have simple one-page app…

…with one button on my main page MainPage.xaml.

My ViewModel which handles all the GUI logic is as follows:

Here you go, app flow is straightforward: If Netflix app is not installed or cannot be started then Google App or App Store web site is open.

My app…

my demo xamarin forms app for opening external apps

… redirecting to App Store or Google play, if Netflix app is not installed.

netflix store web

If app is installed then Netflix client app is started, as shown below.

netflix app

All is working just fine!

Summary

In this blog post I presented how to open external app from Xamarin.Forms application.

This application is also good example how to abstract platform specific functionality (Android/iOS) and use it in common code. Commonly, Xamarin Forms libraries nicely handle this – so developer does not need to deal with any platform specific code/API. But, in any serious Xamarin.Forms application targeting these two platforms, you will sooner or later hit into these kind of problems.

If you find this post useful leave a message below. You can freely download demo code from https://github.com/josipx/Jenx.AppOpenDemo.

Happy coding!

3 thoughts on “Xamarin Forms: Open external application

  1. Compañero, primero que todo felicitarte por tus post, son de mucha ayuda, sobre todo para cuando no se maneja en el mundo de programación móvil, quisiera consultarte sobre algo que me pasó con el post (https://www.jenx.si/2021/02/05/xamarin-forms-open-external-application), lo implementpe y funciona súper Android 10, sin embargo en Android 11 cuando tengo instalada la app que deseo abrir de igual forma me lleva al store y desde ahí debo darle abrir.
    Agradezco me pudieras ayudar con eso, muchas gracias!

    1. Hi @marco.

      For Android 11 (API 30+) you need to put this section in android manifest file:
      <queries>
      <package android:name="com.netflix.mediaclient" />
      </queries>

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.