Skip to main content
In this tutorial, we will leverage Trips to build a Live Activity for iOS applications to monitor and display the progress of a trip from start to finish, the trip destination, and the arrival ETA on a user’s lock screen and Dynamic Island. If you want to see the full project, you can clone the source code in the section below. This tutorial assumes you have a Radar account and an existing iOS app with the Radar SDK implemented. A detailed implementation overview can be found here. Live Activity example lock screen

Source code

GitHub Repo

Languages used

  • Swift
  • SwiftUI

Features used

Steps

1

Add Widget Extension

In Xcode go to File -> New -> Target and search for widget:Live Activity Step 1 setupName your Extension TripActivityExtension and press Finish:Live Activity Step 1 finishThis will create and add a new folder and a few files to your project. We will be working with the TripActivityExtensionLiveActivity file.Live Activity Step 1 file structure
2

Add app entitlements

Add Supports Live Activities and Supports Live Activities Frequent Updates to the info.plist of your main app:Live Activity Step 1 finish
3

Create trip activity manager

In your app target, create a TripLiveActivityManager.swift and define a TripLiveActivityManager class. This is where all of the logic for updating our live activity will live so create a shared instance and add the variables defined below:
4

Add activity handlers

There are three public methods we will use from the TripLiveActivityManager to handle starting the live activity, updating it when a user’s location is updated, and ending it when the trip is completed. Let’s add those.
5

Create private methods for TripActivityManager

Add the private methods to TripActivityManager for creating the activity, building the content state (the data to be displayed in our live activity), and fetching the destination address with the help of Radar.reverseGeocode().
6

Add String Extension

Finally, add a String extension to truncate the destination address for display purposes.
7

Handle Live Activity

Now that we have our activity manager, we can create the logic for handling our Live Activity. in your app’s AppDelegate, add the following function:
8

Add RadarDelegate functions

All that is left for us to do in the app is to hook into the RadarDelegate to listen for location updates and Radar events. First, in applicationDidFinishLaunchingWithOptions set the delegate after Radar.initialize():
and add the delegate methods below in AppDelegate:
9

Build Live Activity views

We now have all of the Live Activity and Radar SDK handlers we need to create and update a Live Activity based on Radar Trip data. All we need to do from here is create the SwiftUI views and helper functions in our TripActivityExtension to display active trip data. This example includes various assets and design choices you may want to change but for the sake of this tutorial the TripActivityExtension file structure is as follows:
Make sure the Target Membership for these files includes your app and the extension
Live Activity Step 9 finish
10

Create LockScreenView

Add Views/LockScreenView.swift
11

Create ModeImageView

Add Views/ModeImageView.swift
12

Create ProgressBar

Add Views/ProgressBar.swift
13

Create RadarArrowImage

Add Views/RadarArrowImage.swift
14

Create TripColors

Add Helpers/TripColors.swift
15

Create TripFormatters

Add Helpers/TripFormatters.swift
16

Add UIColor extension

Add Extensions/UIColor+Hex.swift
17

Download Assets

The assets used in this example Live Activity can be downloaded here. Add them to TripActivityExtension/Assets.
18

Update Main View

Update TripActivityExtensionLiveActivity.swift to use the new views.
We now have a fully functional Live Activity that updates based on Radar Trips data on a user’s lock screen and Dynamic Island!
19

Create a Geofence

With our Live Activity set up, the last thing we need to do is start a trip. If you don’t have any geofences in your Radar project, you will need to add one. For this example I have created the geofence with the identifier trip12345, shown below:Live Activity Step 19 geofence
20

Start a trip

In order to test your new Live Activity, call Radar.startTrip with a destinationGeofenceTag and destinationGeofenceExternalId that match the Geofence in your Radar project. You can also include any additional RadarTripOptions you need. See the example below for reference:

Conclusion

Congratulations on finishing the tutorial! Once you have started a trip, you should see your Live Activity on your lock screen and in your dynamic island. Lock Screen Live Activity example lock screen Dynamic Island Live Activity example dynamic island Expanded Dynamic Island Live Activity example dynamic island expanded

Support

Have questions? Contact us at radar.com/support.