如何检测PKAddPassesViewController中的添加按钮按下

时间:2019-08-26 03:57:33

标签: ios swift pkaddpassesviewcontroller

我试图检测用户何时点击testSources.setAccess(InterfaceB.class, EasyMock.<InterfaceB>mock(InterfaceB.class));中的“添加”按钮。

enter image description here

我添加了PKAddPassesViewController,以便在取消addPassesViewControllerDidFinish()时将调用函数passVC

addPassesViewControllerDidFinish()

但是,当用户拒绝passVC时,func override func viewDidLoad() { self.pass = try PKPass(data: downloadedData! as Data) let passVC = PKAddPassesViewController(pass: self.pass) self.present(passVC!, animated: true) // when passVC is dimissed by the user, addPassesViewControllerDidFinish is expected to be called, but it never gets called. } func addPassesViewControllerDidFinish(_ controller: PKAddPassesViewController) { print("enter DidFinish") let passLib = PKPassLibrary() // Get your pass guard let pass = self.pass else { return } if passLib.containsPass(pass) { print("if start") // Show alert message for example let alertController = UIAlertController(title: "", message: "Successfully added to Wallet", preferredStyle: .alert) alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in controller.dismiss(animated: true, completion: nil) })) controller.show(alertController, sender: nil) print("if end") } else { // Cancel button pressed print("else start"); controller.dismiss(animated: true, completion: nil) print("else end"); } } 根本不会被调用。

1 个答案:

答案 0 :(得分:3)

您应该修复三件事:
1.在ViewController类中扩展PKAddPassesViewControllerDelegate。
2.仔细检查是否已将delegate添加到PKAddPassesViewController:VC?.delegate = self,它将把您的委托链接到PKAddPassesViewControllerDelegate。
3.在addPassesViewControllerDidFinish内,先关闭controller。然后在passLib.containPass内执行任何操作。 alertController不再属于controller,可能不再属于其父视图。