Skip to content

Commit

Permalink
feat: Support for iOS universal links
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Apr 22, 2021
1 parent 8a9bc81 commit 3a7253e
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions src/Uno.UI/UI/Xaml/Application.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#if XAMARIN_IOS
using Foundation;
using System;
using System.Linq;
using UIKit;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Metadata;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.ApplicationModel;
using ObjCRuntime;
using Windows.Graphics.Display;
Expand Down Expand Up @@ -61,7 +59,7 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
{
_preventSecondaryActivationHandling = true;
var url = (NSUrl)urlObject;
if (TryParseActivationUri(url, out var uri))
if (TryParseUri(url, out var uri))
{
OnActivated(new ProtocolActivatedEventArgs(uri, ApplicationExecutionState.NotRunning));
handled = true;
Expand All @@ -74,6 +72,17 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
OnLaunched(new LaunchActivatedEventArgs(ActivationKind.Launch, shortcutItem.Type));
handled = true;
}
else if (
TryGetUserActivityFromLaunchOptions(launchOptions, out var userActivity) &&
userActivity.ActivityType == NSUserActivityType.BrowsingWeb)
{
_preventSecondaryActivationHandling = true;
if (TryParseUri(userActivity.WebPageUrl, out var uri))
{
OnActivated(new ProtocolActivatedEventArgs(uri, ApplicationExecutionState.NotRunning));
handled = true;
}
}
}

// default to normal launch
Expand All @@ -84,12 +93,29 @@ public override bool FinishedLaunching(UIApplication application, NSDictionary l
return true;
}

public override bool ContinueUserActivity(UIApplication application, NSUserActivity userActivity, UIApplicationRestorationHandler completionHandler)
{
// If the application was not running, universal link was already handled by FinishedLaunching
if (!_preventSecondaryActivationHandling)
{
if (userActivity.ActivityType == NSUserActivityType.BrowsingWeb)
{
if (TryParseUri(userActivity.WebPageUrl, out var uri))
{
OnActivated(new ProtocolActivatedEventArgs(uri, ApplicationExecutionState.Running));
}
}
}
_preventSecondaryActivationHandling = false;
return true;
}

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
// If the application was not running, URL was already handled by FinishedLaunching
if (!_preventSecondaryActivationHandling)
{
if (TryParseActivationUri(url, out var uri))
if (TryParseUri(url, out var uri))
{
OnActivated(new ProtocolActivatedEventArgs(uri, ApplicationExecutionState.Running));
}
Expand Down Expand Up @@ -149,7 +175,7 @@ public override UIInterfaceOrientationMask GetSupportedInterfaceOrientations(UIA
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public NSString GetWorkingFolder() => new NSString(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));

private bool TryParseActivationUri(NSUrl url, out Uri uri)
private bool TryParseUri(NSUrl url, out Uri uri)
{
if (Uri.TryCreate(url.ToString(), UriKind.Absolute, out uri))
{
Expand All @@ -161,6 +187,19 @@ private bool TryParseActivationUri(NSUrl url, out Uri uri)
return false;
}
}

private bool TryGetUserActivityFromLaunchOptions(NSDictionary launchOptions, out NSUserActivity userActivity)
{
userActivity = null;

if (launchOptions.TryGetValue(UIApplication.LaunchOptionsUserActivityDictionaryKey, out var userActivityObject) &&
userActivityObject is NSDictionary userActivityDictionary)
{
userActivity = userActivityDictionary.Values.OfType<NSUserActivity>().FirstOrDefault();
}

return userActivity != null;
}
}
}
#endif

0 comments on commit 3a7253e

Please sign in to comment.