Atif
Atif

Reputation: 286

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

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
{

}

Upvotes: 1

Views: 534

Answers (1)

Shawn Presser
Shawn Presser

Reputation: 69

Try putting @objc before func paymentAuthorizationViewControllerDidFinish.

    @objc func paymentAuthorizationViewControllerDidFinish(_ controller: PKPaymentAuthorizationViewController) {
        controller.dismiss(animated: true, completion: nil)
    }

Upvotes: 0

Related Questions