CallKit:如何区分拒绝呼叫和结束连接呼叫

时间:2016-12-02 09:40:03

标签: ios callkit

假设一个应用程序一次支持两个调用。第一个呼叫已连接,第二个呼叫正在进入。其中一个呼叫正在进行"结束呼叫&接听电话","拒绝","保持通话和接听电话"的按钮。

如果是"结束通话&接听电话",表示结束已连接的电话并接听新电话。 如果"拒绝",则意味着拒绝接听电话。

在两种情况下代表

- (void)provider:(CXProvider *)provider performEndCallAction:(CXEndCallAction *)action

被触发。现在,如何知道哪一个呼叫结束,来电或已经连接的呼叫? CXEndCallAction并未说明"拒绝"也不是"已经结束的电话"

有没有人有解决方案?

3 个答案:

答案 0 :(得分:2)

我发现有一个交易清单代表:

- (BOOL)provider:(CXProvider *)provider executeTransaction:(CXTransaction *)transaction
如果有一个事务会触发其他代表CXSetHeldCallActionCXEndCallAction,那么实现此代理将提供足够的信息。所以现在我可以决定触发什么动作,并在必要时绕过其他代表。

这是我的代码,如果其他人正在努力:

- (BOOL)provider:(CXProvider *)provider executeTransaction:(CXTransaction *)transaction
{
NSLog(@"executeTransaction : %@", transaction.debugDescription);
BOOL callEnd = NO;
BOOL callHold= NO;
BOOL callAnswer = NO;

NSPredicate *filter = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [CXEndCallAction class]];
NSArray *ends = [transaction.actions filteredArrayUsingPredicate:filter];
callEnd = [ends count] >= 1;

filter = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [CXAnswerCallAction class]];
NSArray *answer = [transaction.actions filteredArrayUsingPredicate:filter];
callAnswer = [answer count] >= 1;

filter = [NSPredicate predicateWithFormat:@"self isKindOfClass: %@", [CXSetHeldCallAction class]];
NSArray *hold = [transaction.actions filteredArrayUsingPredicate:filter];
callHold = [hold count] >= 1;

if(callEnd && callAnswer)
{
    actionType = kCallKitEndAndAnswer;
    [[MyCallManager sharedManager] endPreviousCallAndAnswerNewCall:^(BOOL succeeded) {
        NSLog(@"end answered call: %@", succeeded ? @"yes" : @"No");
        CXEndCallAction *en = [ends firstObject];
        succeeded ? [en fulfill] : [en fail];

        CXAnswerCallAction *an= [answer firstObject];
        succeeded ? [an fulfill] : [an fail];

    }];
    return YES;
}
else if(callHold && callAnswer)
{
    actionType = kCallKitHoldAndAnswer;

    [[MyCallManager sharedManager] answerCall:^(BOOL succeeded) {
        NSLog(@"hold answered call: %@", succeeded ? @"yes" : @"No");
        CXSetHeldCallAction *ho = [hold firstObject];
        succeeded ? [ho fulfill] : [ho fail];

        CXAnswerCallAction *an= [answer firstObject];
        succeeded ? [an fulfill] : [an fail];
    }];
    return YES;
}
else if([hold count] == 2)
{
    [[MyCallManager sharedManager] swapCalls];
    usleep(25000);
    CXSetHeldCallAction * fi = [hold firstObject];
    CXSetHeldCallAction * la = [hold lastObject];
    [fi fulfill];
    [la fulfill];

    return YES;
}

return NO;
}

答案 1 :(得分:1)

您需要访问CXEndCallAction的callUUID成员。

在正确的对象上调用事务,因此您只需终止与传输的UUID匹配的调用。这意味着您必须将UUID的支持添加到您自己的调用对象模型中。

答案 2 :(得分:0)

您可以使用enum和“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;
}