Daily coverage of WWDC20.
A Swift by Sundell spin-off.

How to get started with App Clips in iOS 14

Published at 11:05 GMT, 24 Jun 2020
Written by: Gui Rambo

If you’re a frequent listener of the Stacktrace podcast, you probably know what I’ve been saying for years now, that I wish apps were more than just an icon on the Home Screen.

With iOS 14, and the introduction of App Clips, Apple is starting to make that happen. App Clips are basically small pieces of functionality from your app that you can offer to users without requiring them to download the entire app.

Don’t be fooled by the word “small” though, because there aren’t that many limitations as to what can be done within an App Clip. Let’s start with what the limitations actually are, and then take a look at what it takes to get a basic App Clip up and running.

App Clip limitations

Being a feature of iOS, it comes as no surprise that Apple took privacy and security very seriously when developing App Clips. That’s why there are some restrictions as to what types of personal data that App Clips can gain access to.

The main limitation in terms of what APIs that are available is that App Clips can’t access health data, so the API for authorizing an app against the HealthKit API will always return false when queried about the authorization status.

Other than that, pretty much every system API is available for App Clips, with the only major limitation being that App Clips cannot exceed 10 megabytes after app thinning, which makes sense given that they’re supposed to be used to quickly interact with a part of an app.

If a user allows your App Clip to access the camera, microphone, or Bluetooth, and then installs your full-size app, then those permissions will be inherited by your app, so that you don’t need to ask for them again. You can also persist user data by using an App Group and a shared container.

Creating an App Clip target

To make an App Clip available, you have to embed it within your existing iOS app — they can’t be distributed separately. After you create your first App Clip target in Xcode, you’re likely going to notice that it’s basically just a regular iOS app target, because it is!

Something that’s probably going to be required for every App Clip out there is to share code between the main app and its embedded App Clip. That can be done in a few different ways, such as by adding source files and resources to both targets in Xcode, or by using shared frameworks.

With the App Clip target selected, you can now run it directly from Xcode. To simulate opening a specific experience within the App Clip, you can go to its scheme settings and set the environment variable _XCAppClipURL to the URL for the experience that you’d like to try out.

How to handle URLs received from an App Clip activation depends on the type of app that you have. For the more common UIScene-based lifecycle, this is an example of what that could look like:

func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
    guard userActivity.activityType == NSUserActivityTypeBrowsingWeb else { return }
    guard let url = userActivity.webpageURL else { return }

    presentExperience(for: url)
}

func presentExperience(for url: URL) {
    // Route user to the appropriate place in your App Clip.
}

And here’s what the above looks like when using the new SwiftUI-based app lifecycle instead:

var body: some Scene {
    WindowGroup {
        ContentView().onContinueUserActivity(NSUserActivityTypeBrowsingWeb) { activity in
            guard let url = activity.webpageURL else { return }
            // Route user to the appropriate place in your App Clip.
        }
    }
}

Associating an App Clip with a domain

The way App Clips are activated is very similar to the way that Universal Links work. You need to include the Associated Domains capability in your app’s main target, and add an entry such as appclips:example.com, using the domain that you’d like to use.

Just as how you have to include the apple-app-site-association (AASA) file to associate your app with a domain for Universal Links, you also need it for App Clips. To support App Clips, a new appclips key has to be included, with the value being a dictionary with the key apps, which in turn contains an array of app identifiers. Feel free to use the AASA file that I made for ChibiStudio as a reference.

Configuring the discovery experience

After you’ve uploaded your first build containing an App Clip to App Store Connect, you’ll be able to configure a custom card that shows up when the App Clip is activated by a user.

In App Store Connect, you’ll also configure which URL prefixes will activate which App Clip. Being prefixes means that you can have parameters in the URL, for example a parameter specifying the ID of the store in which the user activated an App Clip used for shopping.

App Clips can be activated with custom NFC tags, QR codes, or smart app banners on websites. They work very similarly to Universal Links. In fact, if a user activates an App Clip, but they already have the corresponding app installed, the app will be launched just as if it was a Universal Link.

Conclusion

The introduction of App Clips in iOS 14 really brings the idea to life that apps can be a collection of features, not just an icon on the Home Screen. They also present a great new opportunity for developers to make their apps discoverable by users.

Check out these WWDC20 sessions to learn more:

Written by: Gui Rambo