Xcode Apple Pay - PKPaymentAuthorizationViewControllerDelegate issues

1k views Asked by At

I am trying to implement Apple Pay into my application. I am having an issue even establishing the PKPaymentAuthorizationViewControllerDelegate into my ViewController.swift file.

Most code samples have shown it as: class ViewController: UITableViewController, PKPaymentAuthorizationViewControllerDelegate

Here is my code:

class ApplePayVC: UIViewController, PKPaymentAuthorizationViewControllerDelegate {

code

2

There are 2 answers

0
Dean On BEST ANSWER

That because you must implement some of the functions of the delegate.

See in the Apple Documentation : https://developer.apple.com/library/prerelease/ios/documentation/PassKit/Reference/PKPaymentAuthorizationViewControllerDelegate_Ref/index.html

Two functions are required :

  • paymentAuthorizationViewController(_:didAuthorizePayment:completion:)
  • paymentAuthorizationViewControllerDidFinish(_:)

You must override them, and then, the error will disappear from Xcode

0
YU FAN On

Just add the following funcs

    internal func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: ((PKPaymentAuthorizationStatus) -> Void)) {
        completion(PKPaymentAuthorizationStatus.Success)
    }

    internal func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }

OR

Create a new .swift file like this

 import Foundation

 extension UIViewController: PKPaymentAuthorizationViewControllerDelegate {
    public func paymentAuthorizationViewController(controller: PKPaymentAuthorizationViewController, didAuthorizePayment payment: PKPayment, completion: ((PKPaymentAuthorizationStatus) -> Void)) {
        completion(PKPaymentAuthorizationStatus.Success)
    }

    public func paymentAuthorizationViewControllerDidFinish(controller: PKPaymentAuthorizationViewController) {
        controller.dismissViewControllerAnimated(true, completion: nil)
    }
  }