SiriKit

如何使用 SiriKit

INExtension 无法调用 UIApplication

Paste_Image.png

只能在 UIViewController 中调用

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

INExtension

  • 继承 INExtension
  • 实现 ,处理支持的 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 *)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 *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 

@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 *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 *)recipients
                           senders:(nullable NSArray *)senders
                       searchTerms:(nullable NSArray *)searchTerms
                        attributes:(INMessageAttributeOptions)attributes
                     dateTimeRange:(nullable INDateComponentsRange *)dateTimeRange
                       identifiers:(nullable NSArray *)identifiers
           notificationIdentifiers:(nullable NSArray *)notificationIdentifiers
                        groupNames:(nullable NSArray *)groupNames NS_DESIGNATED_INITIALIZER;

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

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

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

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

@property (readonly, copy, nullable, NS_NONATOMIC_IOSONLY) NSArray *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 *identifiers;

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

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

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

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

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

@end

INSearchForMessagesIntentHandling

@protocol INSearchForMessagesIntentHandling 

@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 *resolutionResults))completion NS_SWIFT_NAME(resolveRecipients(forSearchForMessages:with:));

- (void)resolveSendersForSearchForMessages:(INSearchForMessagesIntent *)intent
                            withCompletion:(void (^)(NSArray *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 *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 *)identifiers
                          attribute:(INMessageAttribute)attribute NS_DESIGNATED_INITIALIZER;

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

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

@end

INSetMessageAttributeIntentHandling

@protocol INSetMessageAttributeIntentHandling 

@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

流程

SiriKit_第1张图片
Paste_Image.png

交互

1. 跟 Siri 说

SiriKit_第2张图片
Paste_Image.png

2. Confirm

SiriKit_第3张图片
Paste_Image.png

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

3. Handle

SiriKit_第4张图片
Paste_Image.png

你可能感兴趣的:(SiriKit)