SiriKit

作者: 公爵海恩庭斯 | 来源:发表于2016-08-25 11:55 被阅读460次

如何使用 SiriKit

INExtension 无法调用 UIApplication

Paste_Image.png

只能在 UIViewController 中调用

[self.extensionContext openURL:<#(nonnull NSURL *)#> completionHandler:<#^(BOOL success)completionHandler#>]

INExtension

  • 继承 INExtension
  • 实现 <INIntentHandlerProviding>,处理支持的 INIntents 类型
- (nullable id)handlerForIntent:(INIntent *)intent
{
    if ([intent isKindOfClass:[INSendMessageIntent class]]) {
        return [[WBSendMessageIntentHandler alloc] init];
    }
    
    return nil;
}

INIntents 类型

VoIP calling

  • INSearchCallHistoryIntent // 搜索通话历史
  • INStartAudioCallIntent // 开始音频电话
  • INStartVideoCallIntent // 开始视频电话

Messaging

  • INSearchForMessagesIntent // 查找消息
  • INSendMessageIntent // 发送消息
  • INSetMessageAttributeIntent // 设置消息属性

Payments

  • INRequestPaymentIntent // 收钱
  • INSendPaymentIntent // 支付

Photo

  • INSearchForPhotosIntent // 查找
  • INStartPhotoPlaybackIntent // 查看

Workouts

  • INStartWorkoutIntent // 开始健身
  • INCancelWorkoutIntent // 取消健身
  • INEndWorkoutIntent // 停止健身
  • INPauseWorkoutIntent // 暂停健身
  • INResumeWorkoutIntent // 恢复健身

Ride booking

  • INGetRideStatusIntent // 获取约车状态
  • INListRideOptionsIntent // 获取约车选项
  • INRequestRideIntent // 约车

CarPlay (automotive vendors only)

  • INSaveProfileInCarIntent
  • INSetProfileInCarIntent
  • INSetAudioSourceInCarIntent // 音频源
  • INSetClimateSettingsInCarIntent // 气候
  • INSetDefrosterSettingsInCarIntent // 除霜装置
  • INSetSeatSettingsInCarIntent // 座椅
  • INSetRadioStationIntent

Restaurant reservations (requires additional support from Apple)

  • INBookRestaurantReservationIntent
  • INGetAvailableRestaurantReservationBookingDefaultsIntent
  • INGetAvailableRestaurantReservationBookingsIntent
  • INGetRestaurantGuestIntent
  • INGetUserCurrentRestaurantReservationBookingsIntent

Messaging

INMessagesDomainHandling

  • INSendMessageIntentHandling
  • INSearchForMessagesIntentHandling
  • INSetMessageAttributeIntentHandling

SendMessage

  • 发送给谁
  • 发送内容

INSendMessageIntent

@interface INSendMessageIntent : INIntent

- (instancetype)initWithRecipients:(nullable NSArray<INPerson *> *)recipients
                           content:(nullable NSString *)content
                         groupName:(nullable NSString *)groupName
                       serviceName:(nullable NSString *)serviceName
                            sender:(nullable INPerson *)sender NS_DESIGNATED_INITIALIZER;

// Contacts to whom the message should be sent.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<INPerson *> *recipients;

// Body text of the message.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSString *content;

@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSString *groupName;

// Specified service for the message.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSString *serviceName;

// The person, or account, sending the message.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) INPerson *sender;

@end

INSendMessageIntentHandling

@protocol INSendMessageIntentHandling <NSObject>

@required

/*!
 @brief handling method

 @abstract Execute the task represented by the INSendMessageIntent that's passed in
 @discussion This method is called to actually execute the intent. The app must return a response for this intent.

 @param  sendMessageIntent The input intent
 @param  completion The response handling block takes a INSendMessageIntentResponse containing the details of the result of having executed the intent

 @see  INSendMessageIntentResponse
 */

- (void)handleSendMessage:(INSendMessageIntent *)intent
               completion:(void (^)(INSendMessageIntentResponse *response))completion NS_SWIFT_NAME(handle(sendMessage:completion:));

@optional

/*!
 @brief Confirmation method
 @abstract Validate that this intent is ready for the next step (i.e. handling)
 @discussion These methods are called prior to asking the app to handle the intent. The app should return a response object that contains additional information about the intent, which may be relevant for the system to show the user prior to handling. If unimplemented, the system will assume the intent is valid following resolution, and will assume there is no additional information relevant to this intent.

 @param  sendMessageIntent The input intent
 @param  completion The response block contains an INSendMessageIntentResponse containing additional details about the intent that may be relevant for the system to show the user prior to handling.

 @see INSendMessageIntentResponse

 */

- (void)confirmSendMessage:(INSendMessageIntent *)intent
                completion:(void (^)(INSendMessageIntentResponse *response))completion NS_SWIFT_NAME(confirm(sendMessage:completion:));

/*!
 @brief Resolution methods
 @abstract Determine if this intent is ready for the next step (confirmation)
 @discussion These methods are called to make sure the app extension is capable of handling this intent in its current form. This method is for validating if the intent needs any further fleshing out.

 @param  sendMessageIntent The input intent
 @param  completion The response block contains an INIntentResolutionResult for the parameter being resolved

 @see INIntentResolutionResult

 */

- (void)resolveRecipientsForSendMessage:(INSendMessageIntent *)intent
                         withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion NS_SWIFT_NAME(resolveRecipients(forSendMessage:with:));

- (void)resolveContentForSendMessage:(INSendMessageIntent *)intent
                      withCompletion:(void (^)(INStringResolutionResult *resolutionResult))completion NS_SWIFT_NAME(resolveContent(forSendMessage:with:));

- (void)resolveGroupNameForSendMessage:(INSendMessageIntent *)intent
                        withCompletion:(void (^)(INStringResolutionResult *resolutionResult))completion NS_SWIFT_NAME(resolveGroupName(forSendMessage:with:));

@end

SearchForMessages

INSearchForMessagesIntent

@interface INSearchForMessagesIntent : INIntent

- (instancetype)initWithRecipients:(nullable NSArray<INPerson *> *)recipients
                           senders:(nullable NSArray<INPerson *> *)senders
                       searchTerms:(nullable NSArray<NSString *> *)searchTerms
                        attributes:(INMessageAttributeOptions)attributes
                     dateTimeRange:(nullable INDateComponentsRange *)dateTimeRange
                       identifiers:(nullable NSArray<NSString *> *)identifiers
           notificationIdentifiers:(nullable NSArray<NSString *> *)notificationIdentifiers
                        groupNames:(nullable NSArray<NSString *> *)groupNames NS_DESIGNATED_INITIALIZER;

// Contact that received the messages to be found.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<INPerson *> *recipients;

@property (readonly, assign, NS_NONATOMIC_IOSONLY) INConditionalOperator recipientsOperator;

// Sender of the messages to be found.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<INPerson *> *senders;

@property (readonly, assign, NS_NONATOMIC_IOSONLY) INConditionalOperator sendersOperator;

@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<NSString *> *searchTerms;

@property (readonly, assign, NS_NONATOMIC_IOSONLY) INConditionalOperator searchTermsOperator;

// Attributes of the message to be found.
@property (readonly, assign, NS_NONATOMIC_IOSONLY) INMessageAttributeOptions attributes;

// Time range in which to search for the message.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) INDateComponentsRange *dateTimeRange;

// If available, the identifier of a particular message to be found.
@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<NSString *> *identifiers;

@property (readonly, assign, NS_NONATOMIC_IOSONLY) INConditionalOperator identifiersOperator;

@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<NSString *> *notificationIdentifiers;

@property (readonly, assign, NS_NONATOMIC_IOSONLY) INConditionalOperator notificationIdentifiersOperator;

@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<NSString *> *groupNames;

@property (readonly, assign, NS_NONATOMIC_IOSONLY) INConditionalOperator groupNamesOperator;

@end

INSearchForMessagesIntentHandling

@protocol INSearchForMessagesIntentHandling <NSObject>

@required

/*!
 @brief handling method

 @abstract Execute the task represented by the INSearchForMessagesIntent that's passed in
 @discussion This method is called to actually execute the intent. The app must return a response for this intent.

 @param  searchForMessagesIntent The input intent
 @param  completion The response handling block takes a INSearchForMessagesIntentResponse containing the details of the result of having executed the intent

 @see  INSearchForMessagesIntentResponse
 */

- (void)handleSearchForMessages:(INSearchForMessagesIntent *)intent
                     completion:(void (^)(INSearchForMessagesIntentResponse *response))completion NS_SWIFT_NAME(handle(searchForMessages:completion:));

@optional

/*!
 @brief Confirmation method
 @abstract Validate that this intent is ready for the next step (i.e. handling)
 @discussion These methods are called prior to asking the app to handle the intent. The app should return a response object that contains additional information about the intent, which may be relevant for the system to show the user prior to handling. If unimplemented, the system will assume the intent is valid following resolution, and will assume there is no additional information relevant to this intent.

 @param  searchForMessagesIntent The input intent
 @param  completion The response block contains an INSearchForMessagesIntentResponse containing additional details about the intent that may be relevant for the system to show the user prior to handling.

 @see INSearchForMessagesIntentResponse

 */

- (void)confirmSearchForMessages:(INSearchForMessagesIntent *)intent
                      completion:(void (^)(INSearchForMessagesIntentResponse *response))completion NS_SWIFT_NAME(confirm(searchForMessages:completion:));

/*!
 @brief Resolution methods
 @abstract Determine if this intent is ready for the next step (confirmation)
 @discussion These methods are called to make sure the app extension is capable of handling this intent in its current form. This method is for validating if the intent needs any further fleshing out.

 @param  searchForMessagesIntent The input intent
 @param  completion The response block contains an INIntentResolutionResult for the parameter being resolved

 @see INIntentResolutionResult

 */

- (void)resolveRecipientsForSearchForMessages:(INSearchForMessagesIntent *)intent
                               withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion NS_SWIFT_NAME(resolveRecipients(forSearchForMessages:with:));

- (void)resolveSendersForSearchForMessages:(INSearchForMessagesIntent *)intent
                            withCompletion:(void (^)(NSArray<INPersonResolutionResult *> *resolutionResults))completion NS_SWIFT_NAME(resolveSenders(forSearchForMessages:with:));

- (void)resolveAttributesForSearchForMessages:(INSearchForMessagesIntent *)intent
                               withCompletion:(void (^)(INMessageAttributeOptionsResolutionResult *resolutionResult))completion NS_SWIFT_NAME(resolveAttributes(forSearchForMessages:with:));

- (void)resolveDateTimeRangeForSearchForMessages:(INSearchForMessagesIntent *)intent
                                  withCompletion:(void (^)(INDateComponentsRangeResolutionResult *resolutionResult))completion NS_SWIFT_NAME(resolveDateTimeRange(forSearchForMessages:with:));

- (void)resolveGroupNamesForSearchForMessages:(INSearchForMessagesIntent *)intent
                               withCompletion:(void (^)(NSArray<INStringResolutionResult *> *resolutionResults))completion NS_SWIFT_NAME(resolveGroupNames(forSearchForMessages:with:));

@end

SetMessageAttribute

  • 未知
  • 已读
  • 未读
  • 有旗标
  • 无旗标

INSetMessageAttributeIntent

typedef NS_ENUM(NSInteger, INMessageAttribute) {
    INMessageAttributeUnknown = 0,
    INMessageAttributeRead,
    INMessageAttributeUnread,
    INMessageAttributeFlagged,
    INMessageAttributeUnflagged,
};

@interface INSetMessageAttributeIntent : INIntent

- (instancetype)initWithIdentifiers:(nullable NSArray<NSString *> *)identifiers
                          attribute:(INMessageAttribute)attribute NS_DESIGNATED_INITIALIZER;

@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray<NSString *> *identifiers;

@property (readonly, assign, NS_NONATOMIC_IOSONLY) INMessageAttribute attribute;

@end

INSetMessageAttributeIntentHandling

@protocol INSetMessageAttributeIntentHandling <NSObject>

@required

/*!
 @brief handling method

 @abstract Execute the task represented by the INSetMessageAttributeIntent that's passed in
 @discussion This method is called to actually execute the intent. The app must return a response for this intent.

 @param  setMessageAttributeIntent The input intent
 @param  completion The response handling block takes a INSetMessageAttributeIntentResponse containing the details of the result of having executed the intent

 @see  INSetMessageAttributeIntentResponse
 */

- (void)handleSetMessageAttribute:(INSetMessageAttributeIntent *)intent
                       completion:(void (^)(INSetMessageAttributeIntentResponse *response))completion NS_SWIFT_NAME(handle(setMessageAttribute:completion:));

@optional

/*!
 @brief Confirmation method
 @abstract Validate that this intent is ready for the next step (i.e. handling)
 @discussion These methods are called prior to asking the app to handle the intent. The app should return a response object that contains additional information about the intent, which may be relevant for the system to show the user prior to handling. If unimplemented, the system will assume the intent is valid following resolution, and will assume there is no additional information relevant to this intent.

 @param  setMessageAttributeIntent The input intent
 @param  completion The response block contains an INSetMessageAttributeIntentResponse containing additional details about the intent that may be relevant for the system to show the user prior to handling.

 @see INSetMessageAttributeIntentResponse

 */

- (void)confirmSetMessageAttribute:(INSetMessageAttributeIntent *)intent
                        completion:(void (^)(INSetMessageAttributeIntentResponse *response))completion NS_SWIFT_NAME(confirm(setMessageAttribute:completion:));

/*!
 @brief Resolution methods
 @abstract Determine if this intent is ready for the next step (confirmation)
 @discussion These methods are called to make sure the app extension is capable of handling this intent in its current form. This method is for validating if the intent needs any further fleshing out.

 @param  setMessageAttributeIntent The input intent
 @param  completion The response block contains an INIntentResolutionResult for the parameter being resolved

 @see INIntentResolutionResult

 */

- (void)resolveAttributeForSetMessageAttribute:(INSetMessageAttributeIntent *)intent
                                withCompletion:(void (^)(INMessageAttributeResolutionResult *resolutionResult))completion NS_SWIFT_NAME(resolveAttribute(forSetMessageAttribute:with:));

@end

流程

Paste_Image.png

交互

1. 跟 Siri 说

Paste_Image.png

2. Confirm

Paste_Image.png

用户说出确认信息后,Siri 才会调用 Handle

3. Handle

Paste_Image.png

相关文章

  • 18.iOS10新特性,适配教程,XCode8新特性

    iOS 10 新特性 SiriKit SiriKit的功能非常强大,支持音频、视频、消息发送接收、搜索照片、预订行...

  • Part1: SiriKit 介绍

    在这里放出原文链接地址 Part 1.1 SiriKit 介绍 SiriKit 可以使你的 App 和 App 提...

  • SiriKit

    Siri目前支持以下几个方面:1、语音和视频通话 (VoIP calling)2、发送消息 (Messaging)...

  • SiriKit

    如何使用 SiriKit INExtension 无法调用 UIApplication 只能在 UIViewCon...

  • 我所感兴趣的iOS10新特性

    SiriKit Siri API 的开放自然是 iOS 10 SDK 中最激动人心也是亮眼的特性。SiriKit ...

  • SiriKit使用简介

    介绍SiriKit SiriKit是让你的内容通过Siri展示的一个框架库。当用户向Siri请求特别类型的服务时,...

  • SiriKit 实践

    ios 10 给我们带来了很多惊喜的特性,其中很多为开发者提供的。其中一个就是SiriKit。 Sirikit提供...

  • SiriKit 学习笔记

    SiriKit 学习笔记 未完成 这是什么东西,干什么用的就不说了撒、 SiriKit 是如何工作的? 目前 Si...

  • 详解 SiriKit - SiriKit 教程(Part 2)

    作者:Jameson Quave,原文链接,原文日期:2016-06-20译者:Crystal Sun;校对:nu...

  • SiriKit初探

    SiriKit简介 Siri 是一款苹果 iOS 系统提供的智能语音助手软件,它的全名是 Speech Inter...

网友评论

      本文标题:SiriKit

      本文链接:https://www.haomeiwen.com/subject/qrxasttx.html