Ios6 如何检查PKAddPasseViewController中是否按下了“取消”或“添加”按钮

Ios6 如何检查PKAddPasseViewController中是否按下了“取消”或“添加”按钮,ios6,passbook,Ios6,Passbook,默认情况下,过程加载在PKAddPassesViewController中。有没有办法知道视图上按下了哪个按钮 //this method runs when user either click on the cancel or add button -(void)addPassesViewControllerDidFinish: (PKAddPassesViewController*) controller { [self dismissViewControllerAnimated:

默认情况下,过程加载在
PKAddPassesViewController
中。有没有办法知道视图上按下了哪个按钮

//this method runs when user either click on the cancel or add button

-(void)addPassesViewControllerDidFinish: (PKAddPassesViewController*) controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
我想获取在
PKAddPassesViewController
中按下的按钮的标题。我尝试了下面的代码来访问标题,但是我得到了
null

NSLog(@"Title of button    %@",controller.navigationController.navigationItem.rightBarButtonItem.title);

据我所知,没有,但您可以尝试检索刚才添加的通行证:

- (PKPass *)passWithPassTypeIdentifier:(NSString *)identifierserialNumber:(NSString *)serialNumber;
如果已添加,则返回pass,如果未添加,则返回nil-这有助于推断是否添加了新pass


请注意,除了添加外,右键还可能显示“更新”(如果通行证已经存在,但您的版本有新数据),或者如果您试图重新添加重复通行证,右键将被禁用。

我使用了另一种方法来解决上述问题。 我将在用户单击“添加”或“取消”按钮后,将存折中已存在的通行证数量与新的通行证计数进行比较。如果通行证计数增加 这意味着pass已添加到存折中,否则不会

-(void)addPassesViewControllerDidFinish: (PKAddPassesViewController*) controller{
    PKPassLibrary* passLib = [[PKPassLibrary alloc] init];
    NSArray * passArray = [passLib passes];

    int currentPasses=[passArray count];
    //Here prevPasses are passes already present in the Passbook.You can 
    //initialise it in -(void)viewDidLoad method

    if(currentPasses>prevPasses){
      NSLog(@"Pass Has Been successfully Added");    
    }else{
      NSLog(@"Cancel Button Clicked"); 
    }
 }
//但在更新同一过程的情况下,过程计数不会增加,从而导致执行else part//无论您是点击“取消”还是“升级”按钮。因此,您需要提供一些额外的逻辑来//跟踪它。

试试这个

-(void) addPassesViewControllerDidFinish:(PKAddPassesViewController *)controller {

    if (self.HKPass) {
        PKPassLibrary *pkLibrary = [[PKPassLibrary alloc] init];
        if ([pkLibrary containsPass:self.HKPass]) 
                // add or update clicked
        else 
           // Cancel Clicked   

    }
    [controller dismissModalViewControllerAnimated:YES];

}
谢谢

Swift.4版的答案

不要忘记为PKAddPasseViewController设置委托

func addPassesViewControllerDidFinish(_ controller: PKAddPassesViewController) {
    let passLib = PKPassLibrary()

    // Get your pass
    guard let pass = self.pass else { return }

    if passLib.containsPass(pass) {
        // Add button pressed

        // 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)

    } else {
        // Cancel button pressed
        controller.dismiss(animated: true, completion: nil)
    }
}

苹果真可耻!