ioscallkitpushkit

How to use completion handler of PushKit for reportNewIncomingCallWithUUID


I am using CallKit for my application.

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type withCompletionHandler:(nonnull void (^)(void))completion
{
    NSDictionary *payloadDict = payload.dictionaryPayload[@"aps"];           

    [self reportIncomingCallFrom:callerName withUUID:self.uuidCallkit completion:^(NSError * _Nullable error) {

    }];
    if (completion) {
        completion();
    }
}

My question is: should I write if completion then call completion, or directly call completion();? So which one is correct?

- (void)pushRegistry:(PKPushRegistry *)registry didReceiveIncomingPushWithPayload:(PKPushPayload *)payload forType:(NSString *)type withCompletionHandler:(nonnull void (^)(void))completion
{
    NSDictionary *payloadDict = payload.dictionaryPayload[@"aps"];           

    [self reportIncomingCallFrom:callerName withUUID:self.uuidCallkit completion:^(NSError * _Nullable error) {

    }];

    completion();
}

this is the method of report incoming call.

- (void)reportIncomingCallFrom:(NSString *) from withUUID:(NSUUID *)uuid completion:(nullable void (^)(NSError *_Nullable error))completion
{
    CXHandle *callHandle = [[CXHandle alloc] initWithType:CXHandleTypeGeneric value:from];
    CXCallUpdate *callUpdate = [[CXCallUpdate alloc] init];

    callUpdate.remoteHandle = callHandle;
    callUpdate.supportsDTMF = YES;
    callUpdate.supportsHolding = NO;
    callUpdate.supportsGrouping = NO;
    callUpdate.supportsUngrouping = NO;
    callUpdate.hasVideo = NO;

    NSLog(@"income uuid here for income call %@",uuid);

    [self.callKitProvider reportNewIncomingCallWithUUID: uuid update:callUpdate completion:^(NSError *error) {
        if (!error) {
            NSLog(@"Incoming call successfully reported.");
        } else {
            NSLog(@"Failed to report incoming call successfully: %@.", [error localizedDescription]);
        }

        completion(error);
    }];
}

Sometimes I don't receive the push in background.

I would like to know if my methods above are correct.


Solution

  • In this case, calling completion() directly or checking whether it's not nil before, it's equivalent because completion has been declared to be nonnull.

    So, you can just call completion() directly and if you have an issue, it's not because of that.