Search code examples
xamarin.iosxamarinviewmodelmvvmcrossappdelegate

Open ViewModel from Appdelegate


I am trying to open a specific view in my Xamarin.Ios-Project via a Web-Url. The url-scheme works fine and i also check the url in the AppDelegate class as shown below:

[Register("AppDelegate")]
public partial class AppDelegate : MvxApplicationDelegate
{
    // other methods ...

    public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
    {
        if (url == null) return false;

        var uri = new Uri(url.ToString());
        if(uri.Host.Equals("myscheme-host"))
        {
            var param = GetParameters(uri);

            // What can i do here?
            ShowViewModel<MyViewModel>();

            return true;
        }
    }
}

How can I show my Viewmodel from this point? I tried to set a value in a static class and check it when the next view loads. But when the app is in the background bevore the user clicked the Url-Scheme-Link, no event (ViewDidLoad, ViewWillAppear, ..) will raise.

Any help is appreciated. Thanks a lot


Solution

  • I am using a custom scheme to start an application (something like myscheme-host:xxxx) so what I would write on the OpenUrl is something like this:

        public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
        {
            if (url.AbsoluteString.StartsWith("myscheme-host:"))
            {
                this.window.RootViewController = new MySchemeViewController();
    

    In your case you would probably create a new view model from the "xxxx" parameters and pass that view model to a view controller (whether that is straight from the code or through a resolver that pairs the view model with a controller).