系統支持iOS 12bash
選擇File → New → Target,勾選Include UI Extension,能夠使用自定義UI擴展。app
方法:File → New → Fileide
建立完後以下界面:ui
能夠看到Intent是一個Category,咱們能夠設置類型(標示Intent的做用),添加參數(根據Siri解析命令傳入),添加標題,描述(這些會顯示在Siri喚醒咱們app的時候)。this
編譯的時候系統會自動生成一個子類XXXIntent : INIntent,咱們須要找到這個類,使用這個類來進行咱們的其餘操做。spa
點擊以下圖位置:code
獲取當前權限cdn
INSiriAuthorizationStatus siriStatus = [INPreferences siriAuthorizationStatus];
複製代碼
請求獲取權限視頻
[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {
switch (status) {
case INSiriAuthorizationStatusAuthorized: // 成功獲取權限
NSLog(@"權限獲取成功");
break;
case INSiriAuthorizationStatusDenied: // 成功獲取權限
NSLog(@"權限獲取用戶拒絕");
break;
default:
break;
}
}];
複製代碼
注意添加info.plist(或者多語言)設置提示語,不然權限請求不會彈出。blog
調用系統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地址: