I followed the example in Microsoft documentation to setup app link in a MAUI app (more specifically MAUI hybrid with a web app in .NET 9).
The example ends with an override
protected override async void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
// Show an alert to test that the app link was received.
await Dispatcher.DispatchAsync(async () =>
{
await Windows[0].Page!.DisplayAlert("App link received", uri.ToString(), "OK");
});
Console.WriteLine("App link: " + uri.ToString());
}
and the following text:
In practice, the app link should take users directly to the content represented by the URI, without any prompts, logins, or other interruptions. Therefore, the OnAppLinkRequestReceived override is the location from which to invoke navigation to the content represented by the URI.
The app shows me the link that was used so all is working so far, but I can't understand how to navigate in OnAppLinkRequestReceived as all the examples I found use Shell.Current which is null here.
After more research, I got it working by implementing OnAppLinkRequestReceived to set a _startPage Field containing the requested uri like this:
protected override async void OnAppLinkRequestReceived(Uri uri)
{
base.OnAppLinkRequestReceived(uri);
_startPage = uri.ToString().Replace("app://", "");
}
And updated CreateWindow like this:
protected override Window CreateWindow(IActivationState? activationState)
{
return new Window(new MainPage(_startPage)) { Title = "App" };
}
Using new constructor for the MainPage class that sets the StartPath of the blazorWebView:
public MainPage(string startPath) : this()
{
blazorWebView.StartPath = startPath;
}