系统支持iOS 12bash
选择File → New → Target,勾选Include UI Extension,能够使用自定义UI扩展。markdown
方法:File → New → Fileapp
建立完后以下界面:ide
能够看到Intent是一个Category,咱们能够设置类型(标示Intent的做用),添加参数(根据Siri解析命令传入),添加标题,描述(这些会显示在Siri唤醒咱们app的时候)。oop
编译的时候系统会自动生成一个子类XXXIntent : INIntent,咱们须要找到这个类,使用这个类来进行咱们的其余操做。this
点击以下图位置:spa
获取当前权限code
INSiriAuthorizationStatus siriStatus = [INPreferences siriAuthorizationStatus];
复制代码
请求获取权限orm
[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) { switch (status) { case INSiriAuthorizationStatusAuthorized: // 成功获取权限 NSLog(@"权限获取成功"); break; case INSiriAuthorizationStatusDenied: // 成功获取权限 NSLog(@"权限获取用户拒绝"); break; default: break; } }]; 复制代码
注意添加info.plist(或者多语言)设置提示语,不然权限请求不会弹出。视频
调用系统API,调用以下页面。
代码以下:
GotoPageIntent *intent = [[GotoPageIntent alloc] init]; // GotoPageIntent为咱们自定义的Intent,找不到看上面 intent.suggestedInvocationPhrase = @"打开app"; // 这是建议的提示语,会展现在页面上 INShortcut *shortcurt = [[INShortcut alloc] initWithIntent:intent]; INUIAddVoiceShortcutViewController *addvc = [[INUIAddVoiceShortcutViewController alloc] initWithShortcut:shortcurt]; addvc.delegate = self; [self presentViewController:addvc animated:YES completion:nil]; 复制代码
处理回调:
/*! @abstract Called after the user finishes the setup flow for 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 Called if 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]; } 复制代码
- (void)handleGotoPage:(GotoPageIntent *)intent completion:(void (^)(GotoPageIntentResponse *response))completion NS_SWIFT_NAME(handle(intent:completion:)) {
// GotoPageIntentResponseCodeContinueInApp 打开app
// GotoPageIntentResponseCodeSuccess 回调成功
GotoPageIntentResponse *response = [[GotoPageIntentResponse alloc] initWithCode:GotoPageIntentResponseCodeSuccess userActivity:nil];
completion(response);
}
复制代码
- (id)handlerForIntent:(INIntent *)intent { if ([intent isKindOfClass:[GotoPageIntent class]]) { return [[GotoAppIntentHandler alloc] init]; } return self; } 复制代码
能够自定义Siri呼出app的样式,在IntentViewController中开发。
WWDC视频:
developer.apple.com/videos/play…
苹果Demo地址: