How to check whether cancel or add button is pressed in PKAddPassesViewController

As far as I am aware there is not, but you could always try and retrieve the pass you have just added with:

- (PKPass *)passWithPassTypeIdentifier:(NSString *)identifierserialNumber:(NSString *)serialNumber;

This will return the pass if it was added and nil if not - this could help deduce whether or not a new pass was added.

Note that as well as adding, the right button could be displaying 'Update' (if the pass is already present but your version has new data), or be disabled if you are trying to re-add a duplicate pass.


I have used another approach to solve the above problem. I am comparing the no. of passes already present in the passbook with the new pass count after the user has either clicked on either the add or cancel button.If pass count increases that means pass has been added to the passbook otherwise not.

-(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"); 
    }
 }

//But in case of updating the same pass ,pass count does not increase resulting in execution of else part //whether you are hitting either the cancel or upgrade button.So you need to provide some extra logic for //tracking it.


try this ,

-(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];

}

Thanks

Tags:

Ios6

Passbook