1. 配置项目支持Intents
- 添加Intents.framework:在Xcode项目中,打开
Build Phases
-> Link Binary With Libraries
,点击+
号,搜索并添加Intents.framework
。
- 配置Info.plist:添加
NSSiriUsageDescription
键,描述应用使用Siri的目的,如$(PRODUCT_NAME) 使用Siri来执行任务
。
2. 定义意图
- 创建自定义意图:使用Xcode的
Intents Definition File
(通常名为Intents.intentdefinition
),在其中定义新的意图。例如,假设用户可能发出“查找附近咖啡店并导航过去”这样复杂指令,创建一个意图,包含“查找地点”和“导航到地点”相关参数。
- 定义参数:为意图添加参数,如
location
(地点名称),action
(动作,如导航、查找)等。参数需指定类型(如String
、CLPlacemark
等)和是否必填。
- 生成意图类:定义好意图后,选择
Editor
-> Create NSObject Subclass
,Xcode会自动生成对应的意图类、意图处理类和意图响应类。
3. 解析用户意图
- 实现意图处理类方法:在生成的意图处理类(如
YourIntentHandler
)中,实现handle
方法。例如:
#import "YourIntentHandler.h"
@interface YourIntentHandler ()
@end
@implementation YourIntentHandler
- (void)handleIntent:(YourIntent *)intent completion:(void (^)(YourIntentResponse * _Nonnull))completion {
// 解析参数
NSString *location = intent.location;
NSString *action = intent.action;
// 判断意图动作
if ([action isEqualToString:@"导航"]) {
// 处理导航逻辑
[self navigateToLocation:location completion:^(BOOL success) {
if (success) {
YourIntentResponse *response = [YourIntentResponse successResponseWithResult:@"正在导航到" location];
completion(response);
} else {
YourIntentResponse *response = [YourIntentResponse failureResponseWithError:@"导航失败"];
completion(response);
}
}];
} else if ([action isEqualToString:@"查找"]) {
// 处理查找逻辑
[self findLocation:location completion:^(NSArray<CLPlacemark *> *placemarks, BOOL success) {
if (success) {
YourIntentResponse *response = [YourIntentResponse successResponseWithResult:@"找到地点" location];
completion(response);
} else {
YourIntentResponse *response = [YourIntentResponse failureResponseWithError:@"查找失败"];
completion(response);
}
}];
}
}
- (void)navigateToLocation:(NSString *)location completion:(void (^)(BOOL success))completion {
// 实际导航逻辑,例如使用MapKit
// 这里简单示例返回成功
completion(YES);
}
- (void)findLocation:(NSString *)location completion:(void (^)(NSArray<CLPlacemark *> *placemarks, BOOL success))completion {
// 实际查找逻辑,例如使用Core Location和CLGeocoder
// 这里简单示例返回成功
completion(nil, YES);
}
@end
- 注册意图处理类:在应用启动时,注册意图处理类。在
AppDelegate
的application:didFinishLaunchingWithOptions:
方法中添加:
#import <Intents/Intents.h>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
INInteraction *interaction = [launchOptions valueForKey:UIApplicationLaunchOptionsUserActivityDictionaryKey];
if (interaction) {
[self handleInteraction:interaction];
}
[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {
if (status == INSiriAuthorizationStatusAuthorized) {
[INIntentInteractionCenter defaultCenter].delegate = self;
}
}];
return YES;
}
- (void)handleInteraction:(INInteraction *)interaction {
YourIntentHandler *handler = [[YourIntentHandler alloc] init];
[handler handleIntent:interaction.intent completion:^(INIntentResponse * _Nonnull response) {
interaction.response = response;
[interaction finish];
}];
}
- (void)interactionCenter:(INInteractionCenter *)interactionCenter willExecuteIntent:(INIntent *)intent completion:(void (^)(INIntentResponse * _Nonnull))completion {
YourIntentHandler *handler = [[YourIntentHandler alloc] init];
[handler handleIntent:intent completion:completion];
}
4. 测试
- 使用Siri Shortcuts:在设备上打开
Shortcuts
应用,创建自定义快捷指令,关联定义的意图,模拟复杂语音指令进行测试。
- 调试:通过Xcode的调试工具,查看意图解析和处理过程中的日志信息,确保逻辑正确执行,如参数是否正确解析、动作是否正确处理等。