Sam Vanhoutte
Sam Vanhoutte

Reputation: 3461

Url scheme based link does not seem to work in Blazor MAUI

I am building an app with .NET MAUI and Blazor, that initially targets iOS, but should also support Android, in a next release.

I have, in my Info.plist file added an entry myapp in the CFBundleURLSchemes array. And I use this as a redirect uri from our web portal (open in app, with the href myapp://settings/profile).

What happens, is that iOS comes and asks confirmation if that link can be opened with my app. (see screenshot). But it just opens the app to the page that was previously open. It does not navigate to the Blazor page that is registered with the @page "/settings/profile" directive.

Is this something that is not supported? Or do I have to add something around the routing, here?

Current logic

With the following code in AppDelegate (for iOS), I can intercept that call and access the requested Url from that scheme-link.

public override bool OpenUrl(UIApplication application, NSUrl url, NSDictionary options)
{
    AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url);
    if (url?.Scheme?.Equals(myScheme, StringComparison.InvariantCultureIgnoreCase) ?? false)
    {
        var pageUrl = url.ToString().Replace($"{myScheme}://", "");
        PageSettings.RequestedUri = pageUrl; // This is the static class/var I want to leverage in BlazorWebView
        return base.OpenUrl(application, new NSUrl( pageUrl), options);
    }
    return base.OpenUrl(application, url, options);
}

However, I don't seem to find out how I can enforce the BlazorWebView to navigate to the right uri.

Screenshot

Upvotes: 0

Views: 1345

Answers (1)

Leo
Leo

Reputation: 74

As I know, there is no way to do this currently with MAUI Blazor.

Refer to the documentation maui-blazor documentation, the .NET MAUI Blazor hybrid project template isn't a Shell-based app.

If you want to route to @page "/settings/profile", you could describe some information about where to go in AppDelegate, then set some staic and launch a simple page (MAUI PAGE), get the value and show the Blazor page.

public override bool OpenUrl (UIApplication app, NSUrl url, string sourceApp, NSObject annotation){
    if (url.BaseUrl.Host.Equals ("app.myapp.io")) {
         UIViewController page = new TargetPage().CreateViewController();     
    }          
    return true;
}

Set the homepage to your needs

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:BlazorDemo"
             x:Class="BlazorDemo.TargetPage"
             BackgroundColor="{DynamicResource PageBackgroundColor}">

    <BlazorWebView x:Name="blazorWebView" HostPage="wwwroot/profile.html">
        <BlazorWebView.RootComponents>
            <RootComponent Selector="#app" ComponentType="{x:Type local:Main}" />
        </BlazorWebView.RootComponents>
    </BlazorWebView>

</ContentPage>

Upvotes: 0

Related Questions