user1884623
user1884623

Reputation: 123

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

By default passes are loaded in PKAddPassesViewController. Is there any way to know which button is pressed on the view.

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

-(void)addPassesViewControllerDidFinish: (PKAddPassesViewController*) controller
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

I want to get the title of the button that is pressed in the PKAddPassesViewController. I have tried the below code to access the title but i am getting null.

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

Upvotes: 8

Views: 3116

Answers (5)

Tsz Him Leung
Tsz Him Leung

Reputation: 1

Swift 5.2

the modifiedDate of the pass will be updated when it is added or added again you can get the modifiedDate and then compare it with current date

let pkl:PKPassLibrary = PKPassLibrary()
// get the pass to check from the wallet
if let pass = pkl.pass(withPassTypeIdentifier: "pass.com.example.yourapp", serialNumber: "serialNumber"){
  // get the modified date
  if let modifiedDate = pass.value(forKey: "modifiedDate") as? Date{
    let result = modifiedDate.distance(to: Date())
    // check if the modified date is within an interval
    if result.isLess(than: 2){
      // add is pressed
    }else{
      // cancel is pressed
    }
  }
}else{
    // cancel is pressed
}

Upvotes: 0

user1884623
user1884623

Reputation: 123

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.

Upvotes: 4

Alex Kolovatov
Alex Kolovatov

Reputation: 879

Swift.4 version of Karthikeyan's answer.

Don't forget to set delegate for your PKAddPassesViewController.

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

Upvotes: 2

Karthikeyan
Karthikeyan

Reputation: 1790

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

Upvotes: 3

PassKit
PassKit

Reputation: 12591

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.

Upvotes: 7

Related Questions