1

Getting error compiling

Non-'@objc' method 'paymentAuthorizationViewControllerDidFinish' does not satisfy requirement of '@objc' protocol 'PKPaymentAuthorizationViewControllerDelegate'

If i add @objc before paymentAuthorizationViewControllerDidFinish, than i get new error of

@objc can only be used with members of classes, @objc protocols, and concrete extensions of classes.

I can directly extend Handler with PKPaymentAuthorizationViewControllerDelegate but I have two different class that extends ApplePayable and I don't want to write the same extension for different class

import UIKit
import PassKit

class ApplePayRequestComposer {}

protocol ApplePayable: PKPaymentAuthorizationViewControllerDelegate {

    func applePaymentSheet(composer: ApplePayRequestComposer) -> PKPaymentAuthorizationViewController?
    func processPKPayment(payment:PKPayment, completed:@escaping (_ success:Bool)->())
}


extension ApplePayable {

    func applePaymentSheet(composer: ApplePayRequestComposer) -> PKPaymentAuthorizationViewController?{
        return nil
    }

    func processPKPayment(payment:PKPayment, completed:@escaping (_ success:Bool)->()) {
        completed(false)
    }
}

// PKPaymentAuthorizationViewControllerDelegate implementation
extension ApplePayable
{
    func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        controller.dismiss(animated: true, completion: nil)
    }

    @available(iOS 11.0, *)
    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, handler completion: @escaping (PKPaymentAuthorizationResult) -> Swift.Void) {
        processPKPayment(payment: payment) { success in
            let status = success ? PKPaymentAuthorizationStatus.success:PKPaymentAuthorizationStatus.failure
            let result = PKPaymentAuthorizationResult(status: status, errors: nil)
            completion(result)
        }
    }

    func paymentAuthorizationViewController(_ controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: @escaping (PKPaymentAuthorizationStatus) -> Swift.Void) {
        processPKPayment(payment: payment) { success in
            completion(success ? PKPaymentAuthorizationStatus.success: PKPaymentAuthorizationStatus.failure)
        }
    }
}

class Handler: NSObject, ApplePayable
{

}
Atif
  • 286
  • 3
  • 14
  • You're extending your protocol and adding implementing code to it, is this what you want to do? Shouldn't you extend a class that implements the protocol instead? – Joakim Danielson May 15 '19 at 08:33
  • @JoakimDanielson but what if I want the same extension to be available in a different class, will I have to write the same code twice – Atif May 15 '19 at 08:42
  • Did you find a solution? I am facing the same issue. – user1366265 Aug 03 '20 at 22:34

1 Answers1

0

Try putting @objc before func paymentAuthorizationViewControllerDidFinish.

    @objc func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        controller.dismiss(animated: true, completion: nil)
    }
  • doing so i get new error, objc can only be used with members of classes, objc protocols, and concrete extensions of classes – Atif May 15 '19 at 08:18