VISUAL STUDIO   Windows Mac

App Links for iOS

How to Link to other apps and Handle Incoming links using App Links

PDF for offline use
Sample Code:
Related Articles:

Let us know how you feel about this

Translation Quality


0/250

last updated: 2016-06

This recipe will show you how to link to other apps using App Links, as well as register your app to receive and parse incoming App Links.

Overview

App Links is an open standard for deep-linking between mobile apps.

There are two main parts to implementing App Links in your apps

  1. Linking to other apps - Instead of opening a web link in a browser directly, you should let the App Links navigation take care of how to open a link
  2. Handling incoming links from other apps - You can advertise on web page links App Link metadata which tells other apps how to deep link to the same content in your app.

Linking to other Apps

  • Install the Rivets component from the Component Store.
  • Find any instances where you navigate to a URL in your app (either by UIApplication.OpenUrl or by loading the page into a custom web view) and replace them with this:
Rivets.AppLinks.Navigator.Navigate("http://any.old.url")
  • Your app will now attempt to Navigate to another installed app for the URL using App Links, or will fall back to using UIApplication.OpenUrl if no App Link meta data is found, or no apps for the metadata are installed.

Handling Incoming Links

  • Install the Rivets component from the Component Store if you haven't already done so.

  • Open your app's Info.plist file, and under the Advanced tab, in the URL Types section, add a new URL Type like:
    Image of Info.plist configuration
    Manually edit your Info.plist file and add the following section:

    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>com.example.store</string>
            <key>CFBundleURLTypes</key>
            <string>Viewer</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>example</string>
            </array>
        </dict>
    </array>

  • In your AppDelegate , setup your app to have a Navigation controller which is your window's root view controller, like this:

UINavigationController navController;

public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
    window = new UIWindow (UIScreen.MainScreen.Bounds);

    navController = new UINavigationController ();
    window.RootViewController = navController;

    window.MakeKeyAndVisible ();
    return true;
}
  • Create a view controller to display your product:
public partial class ProductViewController : UIViewController
{
    public ProductViewController (string productId) : base ()
    {
        ProductId = productId;
    }

    public string ProductId { get; set; }

    public override void ViewDidLoad()
    {
        var label = new UILabel(new RectangleF(10, 10, 100, 30));
        label.Text = ProductId ?? "No Product ID Found";

        View.AddSubview(label);
    }
}
  • In your AppDelegate add the following code:
public override bool OpenUrl (UIApplication app, NSUrl url, string sourceApp, NSObject annotation)
{
    var rurl = new Rivets.AppLinkUrl (url.ToString ());

    var id = string.Empty;

    if (rurl.InputQueryParameters.ContainsKey("id"))
        id = rurl.InputQueryParameters ["id"];

    if (rurl.InputUrl.Host.Equals ("products") && !string.IsNullOrEmpty (id)) {
        var c = new ProductViewController (id);
        navController.PushViewController (c, true);
        return true;
    } else {
        navController.PopToRootViewController (true);
        return true;
    }
}
  • When your app is opened with the url example://products?id=12345 the product view controller will be displayed and you should see the id 12345 from the query string displayed.

  • Create an HTML page with the following HTML:

<html>
 <head>
  <title>Product 12345</title>
  <meta property="al:ios:url" content="example://products?id=12345" />
 </head>
 <body>
  <h1>Product 12345</h1>
  <p>This could be a page about Product 12345</p>
 </body>
</html>
  • Publish this page somewhere on the internet, or at a location your iOS app can reach.

  • Test navigating using App Links from your iOS app by calling this method somewhere:

Rivets.AppLinks.Navigator.Navigate("http://location/of/your/html/file.html");

Summary

This recipe will show you how to link to other apps using App Links, as well as register your app to receive and parse incoming App Links.

Xamarin Workbook

If it's not already installed, install the Xamarin Workbooks app first. The workbook file should download automatically, but if it doesn't, just click to start the workbook download manually.