Audio Cast is a simulated podcasting app that plays episodes from its podcast library. This sample code project demonstrates how to:
Donate media intents for podcast episodes that the user has already heard.
Play episodes in the app using Siri Shortcuts.
Show the podcast or playlist containing an episode.
Suggest new podcast episodes to the user through Siri.
The Audio Cast project contains the targets AudioCast and AudioCastIntents. The first is the app that people use to view and play podcast episodes. The second is the Intents app extension that handles INPlayMediaIntent requests.
The project also includes the target AudioCastKit, a framework containing the source code needed by the other targets for tasks such as data management of the podcast library and playback of audio content. Structuring the code in this manner avoids duplicating source code by providing a central location for the shared code. For more information about this code structure, see Structuring Your Code to Support App Extensions.
Configure the Sample Code Project
Before you can run Audio Cast on your iOS device, perform the following steps in Xcode:
Add a name to App Groups for the AudioCast and AudioCastIntents targets. To learn how to add an app group name, see Configure app groups.
Change the value for the variable AppGroup to the name added in the previous step. AppGroup is located in the file UserDefaults+DataSource.swift.
Select a Team account under the Signing section in the project editor for the targets AudioCast and AudioCastIntents.
Donate Media Intents
Before Siri can suggest a shortcut to audio or video content, an app must tell Siri about the content after the user interacts with it. To accomplish this, the app donates an INPlayMediaIntent object representing the media content to Siri. Audio Cast, for instance, makes a donation each time the user plays a podcast episode using the app.
You can group donations into logical containers, which helps you track donations that are similar to each other. To create a grouping, set the INInteraction object’s groupIdentifier property to a value identifying the logical container. For example, Audio Cast sets the property to the identifier of the podcast show or playlist containing the episode, which groups donations by those podcast shows or playlists.
Because Audio Cast groups donations, it’s able to delete all donations associated with a podcast show or playlist after the user unsubscribes from the show or removes the playlist.
Audio Cast can play audio in the background because its Xcode project has the background mode Audio, AirPlay, and Picture in Picture enabled for the AudioCast target. This setting adds the Required Background Mode key to the app’s Info.plist file. To learn more about this capability, see Enabling Background Audio.
Show Podcasts and Playlists
When Siri suggests or invokes a shortcut to Audio Cast, the user can tap the shortcut to get more information about the podcast show or playlist. In order to provide this information to the user, Audio Cast implements the app delegate method application(_:continue:restorationHandler:). After the user taps the shortcut, the system calls this method passing the media intent from the shortcut as part of a system-provided NSUserActivity object. The app uses information from the intent to restore the app to a state where it displays the show or playlist.
Suggest New Media Content
Podcast apps receive content on a regular basis, such as when a new episode becomes available for a podcast the user is listening to. After receiving the new content, the app should suggest the content to Siri by providing INPlayMediaIntent objects for each new episode. To make the suggestion, pass an array of media intents to setSuggestedMediaIntents(_:) on the shared instance of INUpcomingMediaManager. Audio Cast calls this method to add new episodes to the suggestion list, and to remove those episodes that the user already heard from the list.