简体   繁体   中英

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'

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. Eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM