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

时光总嘲笑我的痴心妄想 提交于 2019-12-10 13:25:58

问题


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

回答1:


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.




回答2:


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.




回答3:


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




回答4:


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


来源:https://stackoverflow.com/questions/14068596/how-to-check-whether-cancel-or-add-button-is-pressed-in-pkaddpassesviewcontrolle

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!