IOS Siri和快捷指令打开app
利用场景必要Siri打开应用或者在自定义快捷指令打开应用,并携带内容进入应用。
1.创建Intents文件
1.1 依次点开File->New->File
https://i-blog.csdnimg.cn/direct/700b5047343842acb157d37428359ab4.png
1.2 搜索intent关键字找到 SiriKit Intent Definition File文件
https://i-blog.csdnimg.cn/direct/ef9e7f7572d946a080842d6606dccfc6.pnghttps://i-blog.csdnimg.cn/direct/712a40e2380f4500915ab650182b887b.png
1.3 找到刚才创建的Intent文件,点击+然后New Intent
https://i-blog.csdnimg.cn/direct/ca3fb6db9f954ef5aaf64c9b8df41798.png
1.4 LaunchApp根据本身必要来自定义命名
https://i-blog.csdnimg.cn/direct/c6a4b360621b40a7aeae74dc231f42a8.png
1.5 设置失败和成功提示内容
https://i-blog.csdnimg.cn/direct/7938d09674714f4d9ed4e065977f873d.png
1.6 设置参数(这里设置了一个名为content的属性,可以根据本身需求添加)
https://i-blog.csdnimg.cn/direct/e530594b16f948d59a486df5d1d46150.png
1.7 设置输出content属性
https://i-blog.csdnimg.cn/direct/5b8c7a23a84e4873a44a4931e4a844ae.png
2.创建一个Intent Extension的Targets
2.1 File->New->Targets
https://i-blog.csdnimg.cn/direct/27f74961795746949e8f903594989765.png
2.2 找到Intent Extension并创建
https://i-blog.csdnimg.cn/direct/6aaff99ba3964e77829b549334a09e33.png
2.3 创建Intent Extension
https://i-blog.csdnimg.cn/direct/4970f928b71f4a3f8f2948c89963b8a6.png
2.4 配置Intent Extension到Intents
创建之后可以在项目里面看到
https://i-blog.csdnimg.cn/direct/c650dd23ea974b10a27d86773818d076.png
找到我们前面配置的Intents文件,关联Intent Extension
https://i-blog.csdnimg.cn/direct/d2e30cd7cdf5447bbeb5c1cbd54bdb70.png
2.5 配置IntenHandler.m
导入#import “LaunchAppIntent.h”,关联LaunchAppIntentHandling
https://i-blog.csdnimg.cn/direct/aee01752295b46e2a208c97a77d0b7aa.png
实现LaunchAppIntent的函数
https://i-blog.csdnimg.cn/direct/39c976ffa05d4dba89a01b48f4226f16.png
- (void)handleLaunchApp:(nonnull LaunchAppIntent *)intent completion:(nonnull void (^)(LaunchAppIntentResponse * _Nonnull))completion {
NSUserActivity *userActivity = [ initWithActivityType:NSStringFromClass()];
completion([ initWithCode:LaunchAppIntentResponseCodeContinueInApp userActivity:userActivity]);
}
- (void)confirmLaunchApp:(LaunchAppIntent *)intent completion:(void (^)(LaunchAppIntentResponse * _Nonnull))completion {
NSUserActivity *userActivity = [ initWithActivityType:NSStringFromClass()];
LaunchAppIntentResponse *response = [ initWithCode:LaunchAppIntentResponseCodeReady userActivity:userActivity];
completion(response);
}
- (void)resolveContentForLaunchApp:(nonnull LaunchAppIntent *)intent withCompletion:(nonnull void (^)(INStringResolutionResult * _Nonnull))completion {
if (intent.content != nil && intent.content.length > 0) {
completion();
} else {
completion();
}
}
resolveContentForLaunchApp:是你1.6步调创建的参数content属性,用于代理接收传过来的参数内容。
假如你不走代理模式是不会走这里的,而是会走AppDelegate的application。
2.6 非代理模式下配置AppDelegate的application。
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
if(]){
INInteraction *interaction = userActivity.interaction;
LaunchAppIntent *intent = (LaunchAppIntent *)interaction.intent;
NSLog(@"application========================%@",intent.content);
// 做自己的业务逻辑
return YES;
}
3.配置info.plist
<key>NSSiriUsageDescription</key>
<string>使用Siri控制应用</string>
<key>NSUserActivityTypes</key>
<array>
<string>LaunchAppIntent</string>
</array>
4.调试
4.1 在快捷指令中添加我们的快捷指令
https://i-blog.csdnimg.cn/direct/69e8fa50c9a64dc28597b928e7a31486.png
4.2 输入Launch App
https://i-blog.csdnimg.cn/direct/fb641d569bc147b591d5e229b9b2b261.png
4.3 输入我们必要带进入的属性内容(流程也就结束了)
https://i-blog.csdnimg.cn/direct/ef040c1fb732424cad49e91e91cfc300.png
5.大概出现的错误
5.1 Cycle inside Runner; building could produce unreliable results. This usually can be resolved by moving the shell script phase ‘Thin Binary’ so that it runs before the build phase that depends on its outputs.https://i-blog.csdnimg.cn/direct/70dc8f8b40ad43f2aa17f2c76d3244c0.png
Thin Binary实行时机,必须在Copy Bundle Resources和Embed Foundation Extensions实行完成之后。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]