探索 iOS16 中新 App Intents 框架的演示应用
博彩演示应用程序
i️ 大约
Booky 是一款正在进行的、评论量很大的演示应用程序,旨在探索 Apple 在 iOS 16 中引入的新“App Intents”框架。
此 API 可让您将操作从 App 扩展到操作系统的其他区域,例如“快捷指令”App 和 Siri。
Functionally, Booky is a basic library you can add books to and mark them as read or unread. It’s runnable code that is conceptually and structurally similar to the examples provided in WWDC22’s ‘Dive Into App Intents’ session.
Technically, Booky is built using SwiftUI and persists saved books to a local CoreData store. It has only been tested on iPhone but the code should run on iPad (though the UI hasn’t been optimised for it yet).
When you first open the app, three dummy books will be added to the library for testing. Please note that the functionality is extremely bare bones. Testing, error handling and localization are all currently absent. Hopefully, however, it’ll be a useful playground to help explore some of what the App Intents framework has to offer.
There are some known issues with the app (see ‘Known Issues’ below) – I’ll try and iron these out as the betas progress.
Here are some of the things covered by Booky:
- 5x Shortcut actions
- Foreground & background actions
- Conditional parameter summaries
- Rich confirmation prompts
- Dynamic lookup parameters with thumbnails and subtitles
- Custom entity with queryable properties
- Action library categories
- Custom errors
- Snippet results
⚙️ SHORTCUTS ACTIONS
Booky has five actions (or ‘intents’) that it provides to the Shortcuts app:
ADD BOOK
Add a new book to the library.
This action has properties of various types which support different input options, such as per-word keyboard capitalization.
It displays a SwiftUI ‘Snippet’ UI (functionally similar to a Homescreen widget) showing a preview of the new book when it completes successfully.
It also outputs a rich custom ‘Book’ entity for use in the next action.
MARK BOOKS AS READ
Mark multiple books as either read or unread.
This action has a parameter that accepts an array of a custom Book entities. When tapped, it populates a dynamic, filterable list of Books that display along with images. It is sectioned into a ‘read’ & ‘unread’ section using a DynamicOptionsProvider.
It also demonstrates how to use an enum to create a fixed multiple-choice parameter.
OPEN BOOK
Opens the chosen book in the Booky app or navigates to the root library view.
This action demonstrates how to perform a foreground action in your app from Shortcuts. It opens the app and uses the new programmatic navigation API in SwiftUI to either jump to a specific detail view or clear the navigation stack.
It uses the Switch/Case API in ParameterSummary to display a conditional summary depending on which option is chosen.
DELETE BOOKS ⚠️
Deletes the selected books from the library.
This action demonstrates how to prompt the user with an optional confirmation (containing images in a Snippet) before deleting the books.
It uses the ParameterSummary’s When/Otherwise API.
FIND BOOKS ⚠️
New in iOS 16, this action is automatically added when you add a custom entity with a query that conforms to EntityPropertyQuery (Not sure this is actually correct since it currently shows when conforming to EntityStringQuery/EntityQuery too?).
This action allows the user to query Booky’s database for Books using combinations of property queries. For example: .
Books published after the year 2000 that are unread
✅ REQUIREMENTS
If Booky’s shortcut actions aren’t showing in the Shortcuts app, make sure is pointing to the correct Xcode, as per the Xcode release notes for Dev Beta 1xcode-select
⚠️ KNOWN ISSUES
- The ‘Find Books’ action isn’t working yet. I don’t think it should actually even show when there isn’t a query that conforms to , which there currently isn’t. I’m working on adding conformance to to get it working properly but having some issues. The display name is also not showing correctly (FB10210421)
EntityStringQuery
EntityParameterQuery
- When tapping on an action’s parameter that accepts a custom , the filtering isn’t working. The never seems to get called. (FB10213109)
ShortcutsBookEntity
entities(matching String)
- The Delete Books parameter sumary doesn’t show correctly, instead showing as if none has been set. I believe this is an issue with ‘s When/Otherwise API (FB10208191)
ParameterSummary
- Input options seem to have no effect – for example the Title & Author parameters in the Add Book action should be capitalized by word (FB10200372)
- The text colour in a Snippet view doesn’t show the correct (lighter) colour in dark mode (FB10209882)
- Long-pressing a File parameter in the list UI (as opposed to the parameter summary) has no effect, which makes it impossible to select a magic variable (FB10191345)
🎓 STILL TO EXPLORE
- Booky currently uses singletons for CoreData access and ViewModel changes but it seems the better way to access the app’s state in the App Intent’s ‘perform’ method is by using dependency injection with IntentDependency & IntentDependencyManager
- PredictableIntent protocol
- App Shortcuts – Shortcuts containing a single action that are automatically added by the developer to the Shortcuts app
- Customising donated intents based on user behaviour & specific sets of parameters
- Intent discovery documentation
- Focus Filters
- Resolvers
- Live Activity Snippets
- Siri dialog improvements (for example for errors)
- Localization
- Matching legacy SiriKit intents to prevent duplicates
📚 FURTHER READING
- WWDC SESSIONS
- DOCUMENTATION
- SAMPLE CODE
- Apple’s Food Truck example app. This cross-platform app includes one basic intent that displays a chart in a Snippet. It also implements App Shortcuts.
- RELEASE NOTES
📨 CONTACT
Questions, code contributions & contructive feedback welcome. I don’t have a clue what I’m doing.
You can find me on Twitter: @mralexhay.