Search code examples
iosxamarin.formsxamarin.ioscustom-url

How to handle custom URL scheme in Xamarin Forms


I have a Xamarin Forms Shell app but I'm focusing on the ios platform at the moment.

Example custom url: myawesomeapp:report?id=22

On iOS, I have added the custom URL scheme 'myawesomeapp' to info.plist. And now clicking on the custom url brings up my app successfully and OpenUrl in AppDelegate gets hit.

enter image description here

But how can I process the id=22 to take me to that particular record?

My app structure is using AppShell. I have an ItemsPage.xaml that lists reports and selecting one will bring up that report on ItemDetailPage.xaml. This is the flow that I want to simulate when following the custom url.

Thanks in advance.


Solution

  • Ideal approach would be to create a method in your App.xaml.cs which will accept the given query and then you can call that method from the OpenUrl.

    Here's the example:

    App.xaml.cs

    public void ParseAndPerformNavigation(string query)
    {
       //Do string operation and get the parameter
       //Perform Navigation
    }
    

    Your updated OpenUrl should be something like this:

    public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
    {
       var App = (App)Xamarin.Forms.Application.Current;
       App.ParseAndPerformNavigation(url.AbsoluteString);
       return false;
    }