Search code examples
iosobjective-cswiftcallkitpushkit

Pass objective-c completion handler to swift


Using ios pushkit didReceiveIncomingPushWithPayload; call and pass multiple parameters including withCompletionHandler. What would the function call, interface, and swift arguments look like for withCompletionHandler?

AppDelegate.m

- (void)pushRegistry:(PKPushRegistry *)registry
  didReceiveIncomingPushWithPayload:(PKPushPayload *)payload
             forType:(PKPushType)type
  withCompletionHandler:(void (^)(void))completion{
  NSLog(@"VHC: AppDelegate - didReceiveIncomingPushWithPayload hit!");
  Voip* instance = [Voip new];
  [instance handleIncomingPush:payload forType:type withCompletionHandler:completion];
}

Voip.h

-(void)handleIncomingPush: (PKPushPayload *)payload forType:(PKPushType)type withCompletionHandler:(void (^)(void))completion;

Voip.swift

 @objc(handleIncomingPush: : :)
  func handleIncomingPush(_ payload: PKPushPayload, forType: PKPushType, withCompletionHandler: @escaping () -> Void) {
    withCompletionHandler()
}

Error

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Voip handleIncomingPush:forType:withCompletionHandler:]: unrecognized selector sent to instance 0x2806f4f30'

Solution

  • A couple of observations:

    1. That @objc method name is not right. Given that your Swift method signature matches what you’re looking for in your Objective-C code, you can just remove the manual method name:

      class Voip: NSObject {
          @objc func handleIncomingPush(_ payload: PKPushPayload, forType: PKPushType, withCompletionHandler: @escaping () -> Void) {
              withCompletionHandler()
          }
      }
      

      Or, personally, I’d use a Swiftier method signature and declare that ObjC interface if you want:

      class Voip: NSObject {
          @objc(handleIncomingPush:forType:withCompletionHandler:)
          func handleIncomingPush(_ payload: PKPushPayload, type: PKPushType, completionHandler: @escaping () -> Void) {
              completionHandler()
          }
      }
      
    2. Remove the Voip.h header. Always just used the “Objective-C Generated Interface Header File”.

    3. Instead of importing Voip.h in your Objective-C code, import the generated interface header. E.g. if your app is called MyApp, then it’s likely just:

      #import <MyApp-Swift.h>
      

      Look in your target settings for the “Objective-C Generated Interface Header File” setting if you’re unclear what the name is.