Registering A Uri Scheme

 

Uri Scheme‘s in simple terms are the beginning part of a URI. For example HTTP, or FTP. When you launch a link, the OS or browser or app determines what type of URI it is, then tries to launch the appropriate application.

On a mobile device you can register your own Uri Schemes. For example twitter registers twitter://. This means any link starting with twitter on a mobile device will attempt to find the application that handles it. If it is installed it will open the application. If it is not install, you will most likely be greeted with a message asking if you would like to search for this app in the app store.

Registering one of your mobile app is rather simple and is shown below. Once configured for each platform, the Uri Scheme is registered when the app is installed on the device.

WinRT / UWP

Go to your package.appxmanifest and go to declarations. Add a new protocol and in the Name: section add in your uri scheme.

WindowsProtocol

iOS

Open up your info.pList in an XML editor and add in the following key and values, replacing my-app with your Uri Scheme.

iOSProtocol

Android

Open up your AndroidManifest.xml and add in an intent as below, replacing my-app with your Uri Scheme.

AndroidProtocol1

Pass through parameters

Along with the Uri Scheme you can also pass through parameters or more information. Much like a traditional Uri which contains a domain name then some form of identifier and query string parameters, you can do much the same.

If you do pass through parameters like so, how do you get hold of them in the app?

twitter://compose?text=Hello

There is a place in each platform that allows you to retrieve the value.

WinRT / UWP

In your App.xaml.cs in your native WinRT / UWP app, override the following.

protected override void OnActivated(IActivatedEventArgs args)
{
     if (args.Kind == ActivationKind.Protocol)
     {

         ProtocolActivatedEventArgs eventArgs = args as ProtocolActivatedEventArgs;
         var url = eventArgs.Uri.AbsoluteUri;
     }
     base.OnActivated(args);
}

iOS

In your AppDelegate.cs you just need to override OpenUrl.

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{

}

Android

In the MainActivity.cs file, or the activity that will be loaded when the app starts, in your OnCreate, you can capture the data as shown below.

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    var data = Intent?.Data?.EncodedAuthority;

}

Problems with Uri Scheme

Uri Scheme is an older concept and is not actively promoted as the best way forward today. UWP unfortunately as no other way, however iOS has Universal Links and Android has App Links. While you are still free to use Uri Scheme there are some challenges you may face.

  1. You are not guaranteed the Uri Scheme you choose. Another app can be installed after yours and take over your Uri Scheme. Uri Schemes are registered on who was the last to take the name.
  2. iOS Apps can no longer tell if an app is installed via a Uri Scheme unless the Uri Scheme is listed in the info.pList.
  3. It doesn’t have a fallback if the app isn’t installed, whereas other options such as universal links have a website fallback if the app is not installed.

 

Microsoft MVP (Xamarin) | Exrin | Xamarin Forms Developer | Melbourne, Australia | Open to sponsorship to Canada or US

Related Posts

Leave A Comment?