IOS Siri和快捷指令打开app

打印 上一主题 下一主题

主题 981|帖子 981|积分 2943

利用场景
必要Siri打开应用或者在自定义快捷指令打开应用,并携带内容进入应用。
1.创建Intents文件

1.1 依次点开File->New->File


1.2 搜索intent关键字找到 SiriKit Intent Definition File文件


1.3 找到刚才创建的Intent文件,点击+然后New Intent


1.4 LaunchApp根据本身必要来自定义命名


1.5 设置失败和成功提示内容


1.6 设置参数(这里设置了一个名为content的属性,可以根据本身需求添加)


1.7 设置输出content属性


2.创建一个Intent Extension的Targets

2.1 File->New->Targets


2.2 找到Intent Extension并创建


2.3 创建Intent Extension


2.4 配置Intent Extension到Intents

创建之后可以在项目里面看到

找到我们前面配置的Intents文件,关联Intent Extension

2.5 配置IntenHandler.m

导入#import “LaunchAppIntent.h”,关联LaunchAppIntentHandling

实现LaunchAppIntent的函数

  1. - (void)handleLaunchApp:(nonnull LaunchAppIntent *)intent completion:(nonnull void (^)(LaunchAppIntentResponse * _Nonnull))completion {
  2.     NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([LaunchAppIntent class])];
  3.     completion([[LaunchAppIntentResponse alloc] initWithCode:LaunchAppIntentResponseCodeContinueInApp userActivity:userActivity]);
  4. }
  5. - (void)confirmLaunchApp:(LaunchAppIntent *)intent completion:(void (^)(LaunchAppIntentResponse * _Nonnull))completion {
  6.    
  7.     NSUserActivity *userActivity = [[NSUserActivity alloc] initWithActivityType:NSStringFromClass([LaunchAppIntent class])];
  8.     LaunchAppIntentResponse *response = [[LaunchAppIntentResponse alloc] initWithCode:LaunchAppIntentResponseCodeReady userActivity:userActivity];
  9.     completion(response);
  10. }
  11. - (void)resolveContentForLaunchApp:(nonnull LaunchAppIntent *)intent withCompletion:(nonnull void (^)(INStringResolutionResult * _Nonnull))completion {
  12.     if (intent.content != nil && intent.content.length > 0) {
  13.           completion([INStringResolutionResult successWithResolvedString:intent.content]);
  14.       } else {
  15.           completion([INStringResolutionResult needsValue]);
  16.       }
  17. }
复制代码
resolveContentForLaunchApp:是你1.6步调创建的参数content属性,用于代理接收传过来的参数内容。
假如你不走代理模式是不会走这里的,而是会走AppDelegate的application。
2.6 非代理模式下配置AppDelegate的application。

  1. - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
  2.     if([userActivity.interaction.intent isKindOfClass:[LaunchAppIntent class]]){
  3.         INInteraction *interaction = userActivity.interaction;
  4.         LaunchAppIntent *intent = (LaunchAppIntent *)interaction.intent;
  5.        NSLog(@"application========================%@",intent.content);
  6.         // 做自己的业务逻辑
  7.     return YES;
  8. }
复制代码
3.配置info.plist

  1. <key>NSSiriUsageDescription</key>
  2.                 <string>使用Siri控制应用</string>
  3.                 <key>NSUserActivityTypes</key>
  4.                 <array>
  5.                         <string>LaunchAppIntent</string>
  6.                 </array>
复制代码
4.调试

4.1 在快捷指令中添加我们的快捷指令


4.2 输入Launch App


4.3 输入我们必要带进入的属性内容(流程也就结束了)


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.


Thin Binary实行时机,必须在Copy Bundle Resources和Embed Foundation Extensions实行完成之后。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

耶耶耶耶耶

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表