I’ve been pulling my hair out trying to figure out what I’m doing wrong. I’ll try to explain with as much detail as possible, I have an IOS app that works well with deep links. In the case of deep links if the app is not running and a deep link is clicked, the app gets launched and FinishedLaunching is called with a NSURL in LaunchOptions. If the app is already running openURLContexts is called. So for both cases the app opens the correct location with no problems.
Now, for a more integrated experience, I’m trying to get Universal Links (applinks:domain) to work and simply can’t get them to work properly if the app is not already running. I’ve read through dozens of articles and can't find a step I'm not doing. I'm assuming there is a selector I am missing, but can't seem to find anything else to override/implement. I've added logging code to every method in App/Scene Delegate to capture the run sequence for both cases (app running/app not running). The AASA works and passes validation with both Apple's search validator and branch.IO's validator. In fact the app launches (and works if the app is in background), so I don't think I have any issues with entitlements or the AASA itself.
Here's what happens (links tested from Messages, Notes and Reminders, both on physical devices and simulators, in all cases the results are the same):
If the app is in the background and a Universal Link is opened:
[0:] Will continue user activity
[0:] sceneWilLEnterForegroud
[0:] Continue User Activity - URL Received here
If the app is not running and a Universal Link is opened:
Will Finish Launching and Finished Launching are both called but with null params.
So the Universal Link works, in as much as the app itself is launched, but fails to go to a specific path.
If using deep links both work, with different paths. If already running openURLContexts is called and if not running a URL is passed in finishLoading.
I'm using the latest stable version of Visual Studio 2019 Community and all software is up to date on the Mac, the PC and the iPhone itself.
Thanks in advance,
Dave
App Delegate Code:
public class AppDelegate : UIApplicationDelegate // UIResponder, IUIApplicationDelegate
{
[Export("application:didFailToContinueUserActivityWithType:error:")]
public void DidFailToContinueUserActivitiy(
UIApplication application,
string userActivityType,
NSError error)
{
throw new Exception("");
}
public override void ReceivedLocalNotification(UIApplication application, UILocalNotification notification)
{
AppState.LogDebugOnly("ReceivedLocalNotification");
base.ReceivedLocalNotification(application, notification);
}
public static UIViewController ActiveController;
public override bool ContinueUserActivity(UIKit.UIApplication application, Foundation.NSUserActivity userActivity,
UIKit.UIApplicationRestorationHandler completionHandler)
{
AppState.LogDebugOnly("ContinueUserActivity");
if (userActivity.WebPageUrl != null)
{
AppState.LogDebugOnly(userActivity.WebPageUrl.AbsoluteString);
IOSAppState.appState.HadUrlLaunch = true;
IOSAppState.appState.URLLaunch = userActivity.WebPageUrl.AbsoluteString;
NSNotificationCenter.DefaultCenter.PostNotification(NSNotification.FromName("route", userActivity.WebPageUrl));
}
return true;
}
public override bool WillContinueUserActivity(UIKit.UIApplication application, string userActivityType)
{
AppState.LogDebugOnly($"WillContinueUserActivity {userActivityType}");
return true;
}
public override bool HandleOpenURL(UIApplication application, NSUrl url)
{
var normalizedUrl = Uri.UnescapeDataString(url.ToString());
AppState.LogDebugOnly($"HandleOpenURL {normalizedUrl}");
IOSAppState.appState.HadUrlLaunch = true;
IOSAppState.appState.URLLaunch = url.AbsoluteString;
return true;
}
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
AppState.LogDebugOnly($"OpenURL {url}");
IOSAppState.appState.HadUrlLaunch = true;
IOSAppState.appState.URLLaunch = url.AbsoluteString;
return true;
}
public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
{
AppState.LogDebugOnly($"OpenURL {url}");
return true;
}
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
AppState.LogDebugOnly("Finished Launching " + (launchOptions == null ? "(null) ": launchOptions.ToString()));
if (launchOptions != null)
{
AppState.LogDebugOnly($"Launch options:{launchOptions} ");
}
PerfCounter.Enabled = DebugConfiguration.ProfileApp;
// launch the IOS app state
AppState.appState = new IOSAppState();
DownloadManagerConfiguration.Instance.ResolveRemoteFiles = DebugConfiguration.ResolveDownload;
IOSAppState.appState.Init();
// Override point for customization after application launch.
// If not required for your application you can safely delete this method
return true;
}
public override bool WillFinishLaunching(UIApplication application, NSDictionary launchOptions)
{
AppState.LogDebugOnly($"WillFinishLaunching " + (launchOptions == null ? "(null)" : "(not null)"));
return true;
}
public override UISceneConfiguration GetConfiguration (UIApplication application, UISceneSession connectingSceneSession, UISceneConnectionOptions options)
{
AppState.LogDebugOnly($"GetConfiguration:");
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration.Create ("Default Configuration", connectingSceneSession.Role);
}
public override void DidDiscardSceneSessions (UIApplication application, NSSet<UISceneSession> sceneSessions)
{
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after `FinishedLaunching`.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}
public override void UserActivityUpdated(UIApplication application, NSUserActivity userActivity)
{
AppState.LogDebugOnly("App: UserActivityUpdated");
base.UserActivityUpdated(application, userActivity);
}
public override void DidReceiveRemoteNotification(UIApplication application, NSDictionary userInfo, Action<UIBackgroundFetchResult> completionHandler)
{
base.DidReceiveRemoteNotification(application, userInfo, completionHandler);
AppState.LogDebugOnly("App: DidReceiveRemoteNotification");
}
}
Scene Delegate Code:
[Export("scene:didUpdateUserActivity:")]
public void DidUpdateUserActivity(UIScene scene, NSUserActivity userActivity)
{
AppState.LogDebugOnly("Update User Activity");
}
[Export("scene:willContinueUserActivityWithType:")]
public void WillContinueUserActivity(UIScene scene, string userActivityType)
{
AppState.LogDebugOnly("Will continue user activity");
}
[Export("scene:continueUserActivity:")]
public void ContinueUserActivity(UIScene scene, NSUserActivity userActivity)
{
AppState.LogDebugOnly("Continue User Activity");
if (userActivity.WebPageUrl != null)
{
AppState.LogDebugOnly(userActivity.WebPageUrl.AbsoluteString);
IOSAppState.appState.HadUrlLaunch = true;
IOSAppState.appState.URLLaunch = userActivity.WebPageUrl.AbsoluteString;
NSNotificationCenter.DefaultCenter.PostNotification(NSNotification.FromName("route", userActivity.WebPageUrl));
}
}
[Export ("window")]
public UIWindow Window { get; set; }
[Export("scene:openURLContexts:")]
public void OpenUrlContexts(UIScene scene, NSSet<UIOpenUrlContext> urlContexts)
{
AppState.LogDebugOnly("Scene:openURLContexts" + urlContexts.AnyObject.Url);
AppState.LogDebugOnly($"Open URL {DateTime.Now} {urlContexts.AnyObject.Url}");
var urlString = urlContexts.AnyObject.Url;
IOSAppState.appState.URLLaunch = urlString.AbsoluteString;
IOSAppState.appState.HadUrlLaunch = true;
NSNotificationCenter.DefaultCenter.PostNotification (NSNotification.FromName ("route", urlString));
}
[Export ("scene:willConnectToSession:options:")]
public void WillConnect (UIScene scene, UISceneSession session, UISceneConnectionOptions connectionOptions)
{
AppState.LogDebugOnly("WillConnect");
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see UIApplicationDelegate `GetConfiguration` instead).
if (connectionOptions.UrlContexts != null)
{
//throw new Exception(connectionOptions.UrlContexts.AnyObject.Url.AbsoluteString);
OpenUrlContexts(scene, connectionOptions.UrlContexts);
}
}
[Export ("sceneDidDisconnect:")]
public void DidDisconnect (UIScene scene)
{
AppState.LogDebugOnly("DidDisconnect");
// Called as the scene is being released by the system.
// This occurs shortly after the scene enters the background, or when its session is discarded.
// Release any resources associated with this scene that can be re-created the next time the scene connects.
// The scene may re-connect later, as its session was not neccessarily discarded (see UIApplicationDelegate `DidDiscardSceneSessions` instead).
}
[Export ("sceneDidBecomeActive:")]
public void DidBecomeActive (UIScene scene)
{
AppState.LogDebugOnly("DidBecomeActive");
// Called when the scene has moved from an inactive state to an active state.
// Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive.
}
[Export ("sceneWillResignActive:")]
public void WillResignActive (UIScene scene)
{
AppState.LogDebugOnly("WillResignActive");
// Called when the scene will move from an active state to an inactive state.
// This may occur due to temporary interruptions (ex. an incoming phone call).
}
[Export ("sceneWillEnterForeground:")]
public void WillEnterForeground (UIScene scene)
{
AppState.LogDebugOnly("sceneWilLEnterForegroud");
// Called as the scene transitions from the background to the foreground.
// Use this method to undo the changes made on entering the background.
}
[Export ("sceneDidEnterBackground:")]
public void DidEnterBackground (UIScene scene)
{
AppState.LogDebugOnly("DidEnterBackground");
// Called as the scene transitions from the foreground to the background.
// Use this method to save data, release shared resources, and store enough scene-specific state information
// to restore the scene back to its current state.
}