您可以使用枚举和“isMemberOfClass”。
在 providerDelegate.h
// Call Modes.
typedef NS_ENUM(NSUInteger, CallMode) {
kUnknownMode = 0,
kEndAndAnswerMode = 1,
kHoldAndAnswerMode = 2
};
在 providerDelegate.m
@property(nonatomic, assign) CallMode mode;
并添加以下代码以获取每笔交易。
- (BOOL)provider:(CXProvider *)provider executeTransaction:(CXTransaction *)transaction {
NSLog(@"Provider execute transaction");
BOOL callEnd = NO;
BOOL callHold= NO;
BOOL callAnswer = NO;
NSPredicate *filter = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [CXEndCallAction class]];
NSArray<CXEndCallAction *> *ends = [transaction.actions filteredArrayUsingPredicate:filter];
callEnd = [ends count] >= 1;
filter = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [CXAnswerCallAction class]];
NSArray<CXAnswerCallAction *> *answers = [transaction.actions filteredArrayUsingPredicate:filter];
callAnswer = [answers count] >= 1;
filter = [NSPredicate predicateWithFormat:@"self isMemberOfClass: %@", [CXSetHeldCallAction class]];
NSArray<CXSetHeldCallAction *> *holds = [transaction.actions filteredArrayUsingPredicate:filter];
callHold = [holds count] >= 1;
if(callEnd && callAnswer) {
_mode = kEndAndAnswerMode;
} else if(callHold && callAnswer) {
_mode = kHoldAndAnswerMode;
} else {
_mode = kUnknownMode;
}
return NO;
}