Deep link doesn't work when app is closed

Emixam23Emixam23 USMember ✭✭✭

Hey,

I don't get it, the deeplink is working fine when the app is in background but when it's closed, the app just launch and nothing happens..
Also, since the app is closed at first, I can't debug it (at least I don't know how to do it).

Do you have any idea why it doesn't work?

Best,

Max

Answers

  • Emixam23Emixam23 USMember ✭✭✭

    So I finally found out after a while !

    Actually, the fact that OpenUrl doesn't get called when the app isn't launched yet is pretty logic.

    Example: my_app://query?id=azerty

    • App already in background #launched

      • The OpenUrl(UIApplication app, NSUrl url, NSDictionary options) will get called and in url you can find with which link the app has been launched with.
    • App not running in background #not_launched

      • The FinishedLaunching(UIApplication application, NSDictionary launchOptions) will actually have the url parameter in the launchOptions. The key UIApplicationLaunchOptionsURLKey is then the one you're looking for.

    Code sample:

    public class AppDelegate : UIApplicationDelegate
    {
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            /* ... */
    
            if (launchOptions != null && launchOptions.Count > 0)
            {
                try
                {
                    var url = launchOptions["UIApplicationLaunchOptionsURLKey"] as NSUrl;
                    var result = AppDeepLinksEntry(url);
                }
                catch (Exception ex) { /* ... */ }
            }
    
            /* ... */
        }
    
        #region OpenUrl
    
        public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
        {
            return AppDeepLinksEntry(url);
        }
    
        public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
        {
            return AppDeepLinksEntry(url);
        }
    
        #endregion OpenUrl
    
        private bool AppDeepLinksEntry(NSUrl url)
        {
            Debug.WriteLine($"OpenUrl Url : {url}");
            Debug.WriteLine($"OpenUrl Url Query: {url.Query}");
            Debug.WriteLine($"OpenUrl Url Host: {url.Host}");
            Debug.WriteLine($"OpenUrl Url Path: {url.Path}");
    
            return true;
        }
    }
    

    I hope the explanation and the code sample above helps you to understand and/or solve your future problems !

    Max

  • hyderabadihyderabadi USMember ✭✭✭

    @Emixam23 Can you please explain step by step process of deeplinking, how to start?.

    Thanks in advance.

Sign In or Register to comment.