使用Siri打开自己的应用并触发自己应用的呼叫视频或者语音功能
1.使用Siri这个功能,首先要引入SiriKit这个扩展程序(App Extension).
1.创建一个应用扩展.

在自己的项目下,通过 file->new->target->Intenst Extension. 而在next 后,Product name 设置扩展程序名字,Project选项看是自己的项目吗 ,Embed in Application 是不是自己的项目 如果都是那就点击finish完成.

Add an Intents App Extension to Your Project
Adding an Intents app extension target provides the initial files you need to build your Intents extension and configures your Xcode project to build that extension and include it in your app’s bundle.

Open your existing app project in Xcode.

Choose File > New > Target.

Select Intents extension from the Application Extension group of the iOS or watchOS platform.

Click Next.

Specify the name of your extension and configure the language and other options.

For an iOS app, enable the Include UI Extension option if you plan to customize portions of the Siri interface.

Click Finish.

Note

You may add more than one Intents extension to your app, but each extension must support different intents. Create multiple extensions only if doing so provides a performance advantage or reduces the memory footprint of each extension.

%title插图%num

2.修改默认的Intens事件

Intens事件有

SiriKit groups intents into domains based on the type of app that is likely to support them.

Messaging
Send messages and search the user’s received messages.

Lists and Notes
Create and manage notes and to-do list items.

Workouts
Start, end, and manage fitness routines.

Payments
Send payments between users or pay bills.

VoIP Calling
Initiate calls and search the user’s call history.

Media
Provide media playback.

Visual Codes
Convey contact and payment information using Quick Response (QR) codes.

Photos
Search for and display photos.

Ride Booking
Book rides and report their status.

Car Commands
Manage vehicle door locks and get the vehicle’s status.

CarPlay
Interact with a vehicle’s CarPlay system.

Restaurant Reservations
Create and manage restaurant reservations with help from the Maps app.

需要使用到的是VoIp calling 域 的INStartVideoCallIntent INStartAudioCallIntent两个事件

%title插图%num

将扩展程序的info.plist文件的NSExtension项 默认的IntentsSupported IntentsRestrictedWhileLocked改成这两个

并且还要设置Siri权限 Privacy – Siri Usage Description 和 Privacy – Speech Recognition Usage Description 权限 这两个权限*好也在主程序info.plist里设置一下.

3.IntentHnadler.m 里添加INStartVideoCallIntentHandling和INStartAudioCallIntentHandling

然后实现协议方法

#pragma mark – INStartVideoCallIntentHandling
//解决联系人
– (void)resolveContactsForStartVideoCall:(INStartVideoCallIntent *)intent withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion{
//NSLog(@”呼叫视频:33333333″);
INPerson *peson = [[INPerson alloc] initWithPersonHandle:[[INPersonHandle alloc] initWithValue:@”志愿者” type:INPersonHandleTypeUnknown] nameComponents:nil displayName:@”志愿者” image:nil contactIdentifier:@”志愿者” customIdentifier:@”志愿者”];
INPersonResolutionResult *result = [INPersonResolutionResult successWithResolvedPerson:peson];
completion(@[result]);
}
//
– (void)confirmStartVideoCall:(INStartVideoCallIntent *)intent completion:(void (^)(INStartVideoCallIntentResponse *response))completion{
//NSLog(@”呼叫视频:22222222″);
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartVideoCallIntent class])];
userActivity.title = @”志愿者”;
userActivity.keywords = [NSSet setWithArray:@[@”测试人员”, @”测试”, @”测试”]];
userActivity.eligibleForHandoff = YES;
userActivity.eligibleForSearch = YES;
INStartVideoCallIntentResponse *response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeReady userActivity:userActivity];
completion(response);
}
– (void)handleStartVideoCall:(INStartVideoCallIntent *)intent completion:(void (^)(INStartVideoCallIntentResponse *response))completion{
//NSLog(@”呼叫视频:11111111″);
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartVideoCallIntent class])];
userActivity.title = @”测试”;
userActivity.keywords = [NSSet setWithArray:@[@”测试”, @”测试”, @”测试”]];
userActivity.eligibleForHandoff = YES;
userActivity.eligibleForSearch = YES;
INStartVideoCallIntentResponse *response = [[INStartVideoCallIntentResponse alloc] initWithCode:INStartVideoCallIntentResponseCodeContinueInApp userActivity:userActivity];
completion(response);
}

#pragma mark – INStartAudioCallIntentHandling
– (void)resolveContactsForStartAudioCall:(INStartAudioCallIntent *)intent withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion{
//NSLog(@”呼叫电话:33333333″);
INPerson *peson = [[INPerson alloc] initWithPersonHandle:[[INPersonHandle alloc] initWithValue:@”测试” type:INPersonHandleTypeUnknown] nameComponents:nil displayName:@”测试” image:nil contactIdentifier:@”测试” customIdentifier:@”测试”];
INPersonResolutionResult *result = [INPersonResolutionResult successWithResolvedPerson:peson];
completion(@[result]);
}
– (void)confirmStartAudioCall:(INStartAudioCallIntent *)intent completion:(void (^)(INStartAudioCallIntentResponse *response))completion{
//NSLog(@”呼叫电话:22222222″);
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartVideoCallIntent class])];
userActivity.title = @”测试”;
userActivity.keywords = [NSSet setWithArray:@[@”测试”, @”测试”, @”测试”]];
userActivity.eligibleForHandoff = YES;
userActivity.eligibleForSearch = YES;
INStartAudioCallIntentResponse *response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeReady userActivity:userActivity];
completion(response);
}
//
– (void)handleStartAudioCall:(INStartAudioCallIntent *)intent completion:(void (^)(INStartAudioCallIntentResponse *response))completion{
//NSLog(@”呼叫电话:11111111″);
NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([INStartVideoCallIntent class])];
userActivity.title = @”测试”;
userActivity.keywords = [NSSet setWithArray:@[@”测试”, @”测试”, @”测试”]];
userActivity.eligibleForHandoff = YES;
userActivity.eligibleForSearch = YES;
INStartAudioCallIntentResponse *response = [[INStartAudioCallIntentResponse alloc] initWithCode:INStartAudioCallIntentResponseCodeContinueInApp userActivity:userActivity];
completion(response);
}
4.权限设置

#import <Intents/Intents.h>
//Siri
if (@available(iOS 10.0, *)) {
[INPreferences requestSiriAuthorization:^(INSiriAuthorizationStatus status) {
switch (status) {
case INSiriAuthorizationStatusNotDetermined:
NSLog(@”用户尚未对该应用程序作出选择。”);
break;
case INSiriAuthorizationStatusRestricted:
NSLog(@”此应用程序无权使用Siri服务”);
break;
case INSiriAuthorizationStatusDenied:
NSLog(@”用户已明确拒*此应用程序的授权”);
break;
case INSiriAuthorizationStatusAuthorized:
NSLog(@”用户可以使用此应用程序的授权”);
break;
default:
break;
}
}];
} else {
// Fallback on earlier versions
}

5.在主程序也就是AppDelegate里调用回调监听这个事件

– (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler{
NSLog(@”视频呼叫:触发了%@”,userActivity.title);
return YES;
}

6.出现不在通讯录事件,首先查看扩展程序target版本应该设置低一些不超出

%title插图%num