1

I am trying to create some functionality when a users links into the app. I have created the apple-app-site-association file on the server. It is correctly recognized as an app link and opens my app. The issue is the OnAppLinkRequestReceived event doesn't seem to get triggered.

In my App.xaml.cs:

 public App()
 {
     InitializeComponent();

     MainPage = new AppShell();
 #if IOS
     (Application.Current as IApplicationController)?.SetAppIndexingProvider(new Microsoft.Maui.Controls.Compatibility.Platform.iOS.IOSAppIndexingProvider());
 #endif
        try
        {
           var entry = new AppLinkEntry
            {
                Title = "Find Shop",
                Description = "Find Shop Deep Link",
                AppLinkUri = new Uri(GlobalVariables.FindShopDeepLink, UriKind.RelativeOrAbsolute),
                IsLinkActive = true
            };

            entry.KeyValues.Add("contentType", "id");
            entry.KeyValues.Add("appName", "PopUp Shop");
            entry.KeyValues.Add("companyName", "Cefworx, LLC");

            Application.Current.AppLinks.RegisterLink(entry);
        }
        catch (Exception ex)
        {
            Crashes.TrackError(ex);
            Crashes.TrackError(new Exception("Deeplink failed."));
        }

    }


protected async override void OnAppLinkRequestReceived(Uri uri)
{            
     await Current.MainPage.DisplayAlert("AppLink", "You are linking!", "OK");
}

In my Maui.Program:

public static MauiApp CreateMauiApp()
{
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiCompatibility()
            .UseMauiApp<App>(
            .UseMauiCommunityToolkit();
    .....
}  
Cef
  • 661
  • 1
  • 6
  • 26

2 Answers2

0

According to this similar issue about AppLinks doesnt work on iOS on the github, you can try to override the OpenUrl method the AppDelegate.cs. Such as:

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    Microsoft.Maui.Controls.Application.Current.SendOnAppLinkRequestReceived(uri); 
    return true;
}

Update1:

You can try to add the following code into the MauiProgram.cs to call SendOnAppLinkRequestReceived(uri) when the app opened by the link.

builder
                  .UseMauiApp<App>()
            .ConfigureLifecycleEvents(events =>
            {
#if IOS
                    events.AddiOS(ios => ios
                        .OpenUrl((app,url,opion) => LogEvent(app, url, opion)));

                static bool LogEvent(UIKit.UIApplication application, Foundation.NSUrl url, Foundation.NSDictionary options)
                {
                    Microsoft.Maui.Controls.Application.Current.SendOnAppLinkRequestReceived(url);
                    return true;
                }
#endif
            })

And you can also try to add [Export("application:openURL:options:")] to the OpenUrl when you override it. Such as:

[Export("application:openURL:options:")]
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
    Microsoft.Maui.Controls.Application.Current.SendOnAppLinkRequestReceived(uri); 
    return true;
}

Update2:

According to this case about Method 'application:openURL:options:' is not called, the ios 13.0 and higher will not call the openurl method in the AppDelegate. It call the SceneDelegate instead of the AppDelegate. So you can try the following code to use the SceneOpenUrl event.

.ConfigureLifecycleEvents(events =>
            {
#if IOS
                        if(int.Parse(UIDevice.CurrentDevice.SystemVersion) >12)
                        {
                    events.AddiOS(ios => ios
                        .SceneOpenUrl((parameter1,parameter2) => LogEvent(parameter1,parameter2)));

                static bool LogEvent(UIKit.UIScene scene, Foundation.NSSet<UIKit.UIOpenUrlContext> context)
                {
                              var url =context.ToArray().First().Url;
                              Microsoft.Maui.Controls.Application.Current.SendOnAppLinkRequestReceived(url);
                    return true;
                }
                        }
 #endif      

In addition, you can refer to this case about Xamarin.Forms for iOS using SceneDelegate.cs to use the ContinueUserActivity method in the AppDelegate.cs or use the SceneDelegate instead of the AppDelegate.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • Thanks for the suggestion. I added it but neither the OpenUrl nor the OnAppLinkRequestReceived get called on opening the link. – Cef Apr 18 '23 at 04:17
  • You can check the update part in my answer. @Cef – Liyun Zhang - MSFT Apr 18 '23 at 05:41
  • 1
    In addition, did you use any other 3rd party sdk such as firebase? There was [an case have the same problem as yours](https://stackoverflow.com/questions/56603781/appdelegate-continueuseractivity-not-called-when-launching-app-from-deep-link-f). You can check it. @Cef – Liyun Zhang - MSFT Apr 18 '23 at 05:55
  • No Firebase. I am using Telerik but that's just for UI controls. – Cef Apr 18 '23 at 17:15
  • I made the changes and tried both on the simulator and a physical device but still neither of the events get triggered, even though the link does switch over to the App. – Cef Apr 18 '23 at 17:28
  • This is because the update in the ios 13.0. You can test it in the ios 11.0 and check the update part 2 in my answer. @Cef – Liyun Zhang - MSFT Apr 19 '23 at 07:25
  • In addtion, if this still doesn't work , you can post an issue on the github. @Cef – Liyun Zhang - MSFT Apr 19 '23 at 07:26
  • Thanks. That didn't work but i opened a bug: https://github.com/dotnet/maui/issues/14671 with a sample app. – Cef Apr 19 '23 at 18:42
  • You are welcome. You can follow up it. – Liyun Zhang - MSFT Apr 20 '23 at 09:24
0

From this link: https://github.com/dotnet/maui/issues/14671

iOS: Add to Platform/iOS/AppDelegate.cs

public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{
    
    CheckForAppLink(userActivity);
    return true;
}
void CheckForAppLink(NSUserActivity userActivity)
{
    var strLink = string.Empty;

    switch (userActivity.ActivityType)
    {
    case "NSUserActivityTypeBrowsingWeb":
        strLink = userActivity.WebPageUrl.AbsoluteString;
        break;
    case "com.apple.corespotlightitem":
        if (userActivity.UserInfo.ContainsKey(CSSearchableItem.ActivityIdentifier))
            strLink = userActivity.UserInfo.ObjectForKey(CSSearchableItem.ActivityIdentifier).ToString();
        break;
    default:
        if (userActivity.UserInfo.ContainsKey(new NSString("link")))
            strLink = userActivity.UserInfo[new NSString("link")].ToString();
        break;
    }

    if (!string.IsNullOrEmpty(strLink))
        App.Current.SendOnAppLinkRequestReceived(new Uri(strLink));
}

Android: Add to "Platform/Android/MainActivity.cs"

protected override void OnNewIntent(Intent intent)
{
    base.OnNewIntent(intent);

    string action = intent.Action;
    string strLink = intent.DataString;
    if (Intent.ActionView != action || string.IsNullOrWhiteSpace(strLink))
    return;

    var link = new Uri(strLink);
    App.Current.SendOnAppLinkRequestReceived(link);
}
Cef
  • 661
  • 1
  • 6
  • 26