Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon Can you provide an example of using custom schemes for deep linking in MvvmCross


Can you provide an example of using custom schemes for deep linking in MvvmCross


Using Custom Schemes for Deep Linking in MvvmCross

Deep linking in MvvmCross allows you to navigate to specific views within your app using custom URL schemes. This is particularly useful for launching the app from external links, such as emails or websites, and directing users to specific content.

Step 1: Registering Custom Schemes

To implement deep linking, you first need to register a custom scheme in your app's configuration files:

- Android: In the `AndroidManifest.xml`, add an intent filter to your activity to handle the custom scheme. For example, if your scheme is `mvx://`, you would add something like this:

xml
  
      
          
          
          
          
      
  
  

- iOS: In the `Info.plist`, add a URL type with your custom scheme. You can do this by clicking the "Add URL Type" button and setting the Identifier and URL Schemes.

xml
  CFBundleURLTypes
  
      
          CFBundleTypeRole
          Editor
          CFBundleURLName
          com.yourcompany.yourapp
          CFBundleURLSchemes
          
              mvx
          
      
  
  

Step 2: Handling Deep Links

Next, you need to handle these deep links in your app:

- Android: Override the `OnNewIntent` method in your main activity to handle incoming intents with your custom scheme.

csharp
  protected override void OnNewIntent(Intent intent)
  {
      base.OnNewIntent(intent);
      // Handle the intent here
      var uri = intent.Data;
      if (uri != null && uri.Scheme == "mvx")
      {
          // Use MvvmCross routing service to navigate
          var routingService = Mvx.Resolve();
          routingService.RouteAsync(uri.ToString());
      }
  }
  

- iOS: Override the `OpenUrl` method in the `AppDelegate.cs` to handle URLs.

csharp
  public override bool OpenUrl(UIApplication application, NSURL url, string sourceApplication, NSObject annotation)
  {
      if (url.Scheme == "mvx")
      {
          // Use MvvmCross routing service to navigate
          var routingService = Mvx.Resolve();
          routingService.RouteAsync(url.AbsoluteString);
      }
      return true;
  }
  

Step 3: Defining Routes

In MvvmCross, you define routes using attributes on your view models. These attributes specify the URI pattern that should trigger the navigation to a particular view model.

csharp
[assembly: MvxRouting(typeof(ViewModelA), @"mvx://test/\?id=(?[A-Z0-9]{32})$")]
namespace YourApp.ViewModels
{
    public class ViewModelA : MvxViewModel
    {
        public void Init(string id)
        {
            // Use the captured group (id) here
        }
    }
}

Step 4: Navigating with Parameters

You can navigate between view models using the routing service and pass parameters captured from the URI.

csharp
public class MainViewModel : MvxViewModel
{
    private readonly IMvxRoutingService _routingService;

    public MainViewModel(IMvxRoutingService routingService)
    {
        _routingService = routingService;
    }

    private ICommand _showACommand;

    public ICommand ShowACommand
    {
        get
        {
            return _showACommand ?? (_showACommand = new MvxAsyncCommand(async () =>
            {
                await _routingService.RouteAsync("mvx://test/?id=" + Guid.NewGuid().ToString("N"));
            }));
        }
    }
}

Conclusion

Using custom schemes for deep linking in MvvmCross allows you to seamlessly navigate users to specific parts of your app from external links. By registering custom schemes, handling deep links in your app, defining routes, and navigating with parameters, you can enhance the user experience and provide direct access to app content.

Citations:
[1] https://github.com/charri/MvvmCross-Routing
[2] https://stackoverflow.com/questions/20946531/mvvmcross-deep-linking
[3] https://bankapi.net/demdemo/site/docs/quick-starts/deep-link/index.html
[4] https://www.branch.io/glossary/deep-linking/
[5] https://www.mvvmcross.com/documentation/fundamentals/navigation
[6] https://www.mvvmcross.com/documentation/advanced/customizing-using-App-and-Setup
[7] https://www.mvvmcross.com/documentation/getting-started/getting-started
[8] https://www.googlecloudcommunity.com/gc/Tips-Tricks/META-POST-Deep-Links-URL-Parameters/m-p/349632
[9] https://developers.helpshift.com/android/deep-linking/
[10] https://xamarincodes.wordpress.com/2020/04/18/deep-links-in-xamarin-forms/