The system supports iOS 12
Engineering structures,
Enabling Project Rights
Add a new Target
Select File → New → Target and check Include UI Extension to use custom UI Extension.
Create an Intent Definition File
Method: File → New → File
The new Intent
After creation, the following interface is displayed:
As you can see, the Intent is a Category. We can set the type (indicating what the Intent does), add parameters (passed in according to the Siri parse command), add a title, and add a description (which will be displayed when Siri wakes up our app).
At compile time, the system automatically generates a subclass XXXIntent: INIntent, which we need to find and use for our other operations.
Click the following location:
The development process
Access to
Obtain current permissions
INSiriAuthorizationStatus siriStatus = [INPreferences siriAuthorizationStatus];
Copy the code
Request permission
[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {
switch (status) {
caseINSiriAuthorizationStatusAuthorized: / / success is get NSLog (@"Permission obtained successfully");
break;
caseINSiriAuthorizationStatusDenied: / / success is get NSLog (@"Permission acquisition user denied");
break;
default:
break; }}];Copy the code
Note to add info.plist (or multilingual) setting prompt, otherwise the permission request will not pop up.
Add Siri shortcuts page
Call the system API, call the following page.
The code is as follows:
GotoPageIntent *intent = [[GotoPageIntent alloc] init]; / / GotoPageIntent custom Intent for us, can't find see above Intent. SuggestedInvocationPhrase = @"Open the app"; INShortcut *shortcurt = [[INShortcut alloc] initWithIntent:intent]; INUIAddVoiceShortcutViewController *addvc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortcurt]; addvc.delegate = self; [self presentViewController:addvc animated:YES completion:nil];Copy the code
Handling callbacks:
/ *! @abstract Called after the user finishes the setup flowfor the voice shortcut, with either the successfully-added voice shortcut, or an error.
@discussion Your implementation of this method should dismiss the view controller.
*/
- (void)addVoiceShortcutViewController:(INUIAddVoiceShortcutViewController *)controller didFinishWithVoiceShortcut:(nullable INVoiceShortcut *)voiceShortcut error:(nullable NSError *)error; {
if(! error) { [controller dismissViewControllerAnimated:YES completion:nil]; }} / *! @abstract Calledif the user cancels the setup flow; the voice shortcut was not added.
@discussion Your implementation of this method should dismiss the view controller.
*/
- (void)addVoiceShortcutViewControllerDidCancel:(INUIAddVoiceShortcutViewController *)controller; {
[controller dismissViewControllerAnimated:YES completion:nil];
}
Copy the code
Handle callbacks triggered by Siri Shortcuts
- Custom xxxIntentHandler, inherited from NSObject. Follow xxxIntentHandling and implement the desired logic in the Intent Definition File.
- (void)handleGotoPage:(GotoPageIntent *)intent completion:(void (^)(GotoPageIntentResponse *response))completion NS_SWIFT_NAME (handle (intent: completion:)) {/ / GotoPageIntentResponseCodeContinueInApp open the app / / GotoPageIntentResponseCodeSuccess callback success GotoPageIntentResponse * response = [[GotoPageIntentResponse alloc] initWithCode:GotoPageIntentResponseCodeSuccess userActivity:nil]; completion(response); }Copy the code
- Open up our Extension IntentHander method. Handle our own defined intents.
- (id)handlerForIntent:(INIntent *)intent {
if ([intent isKindOfClass:[GotoPageIntent class]]) {
return [[GotoAppIntentHandler alloc] init];
}
return self;
}
Copy the code
Custom ExtensionUI
Custom Siri call app style, developed in IntentViewController.
The resources
WWDC video:
Developer.apple.com/videos/play…
Apple Demo address:
Developer.apple.com/documentati…
Documents: developer.apple.com/documentati…