Reputation: 929
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();
.....
}
Upvotes: 1
Views: 1627
Reputation: 929
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);
}
Upvotes: 1
Reputation: 14519
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.
Upvotes: 0