App Clips for iOS: Getting Started

In this tutorial, you’ll learn how to design and implement App Clips. By Graham Connolly.

Leave a rating/review
Download materials
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Using Ephemeral Notifications

Like a main app, App Clips can receive notifications. These can add great value to App Clips by notifying you when your order is ready. App Clips can receive notifications only for a short time after getting launched — up to eight hours.

Open the Info.plist of your App Clip and enable ephemeral notifications — it’s in the same place as the location confirmation permission:

Enabling notification

This enables notifications for the App Clip, but the user can opt in on the App Clip Card. As a result, in SwiftyLemonadeClipApp.swift, add the following code below handleUserActivity(_:):

func requestNotificationAuthorization() {
  //1
  let notifCenter = UNUserNotificationCenter.current()
  notifCenter.getNotificationSettings { setting in
    //2
    if setting.authorizationStatus == .ephemeral {
      return
    }
    //3
    notifCenter.requestAuthorization(options: .alert) { result, error  in
      print("""
        Authorization Request result: \(result) \
        - \(String(describing: error))
        """)
    }
  }
}

This code:

  1. Retrieves the notification settings for the app
  2. Checks to see the if app is authorized to receive ephemeral notifications. If access is already granted, then there is no need to continue. But if access is not granted, then request it again.

Finally, while still in SwiftyLemonadeClipApp.swift, add the following code to the end of body. Be sure it appears inside the closing brace for WindowGroup:

//1
.onAppear {
  requestNotificationAuthorization()
}

This code calls requestNotificationAuthorization() when this view appears.

There you go! You don’t need to miss all the soccer action now, with notifications enabled.

Note: If you would like to learn more about App Clip notifications, check out Apple’s Documentation, or our Push Notifications Tutorial.

Where to Go From Here?

You can download the completed project files by clicking the Download Materials button at the top or bottom of the tutorial.

In this tutorial, you learned how to:

  1. Add an App Clip target
  2. Share assets and code
  3. Use the Location Confirmation API to verify you are at the correct location
  4. Set up App Clip notifications

If you enjoyed this tutorial, check out SwiftUI by Tutorials. You’ll take a deep dive into how to define what your app’s UI should do with concise, declarative language, and say goodbye to tons of confusing UIKit code.

If you are interested in learning more about App Clips, check out Apple’s Documentation.

If you have any questions or comments, join the forum discussion below.