7

默认情况下,通行证加载到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);
4

5 回答 5

6

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

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

如果添加了通行证,这将返回通行证,如果没有,则返回 nil - 这可以帮助推断是否添加了新通行证。

请注意,除了添加之外,右侧按钮可能会显示“更新”(如果该通行证已经存在但您的版本有新数据),或者如果您尝试重新添加重复的通行证,则该按钮被禁用。

于 2013-01-02T07:28:05.650 回答
4

我使用了另一种方法来解决上述问题。我比较的是没有。在用户单击添加或取消按钮后,存折中已经存在的通行证和新的通行证计数。如果通行证计数增加,则表示通行证已添加到存折中,否则不会。

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

//但是如果更新相同的pass,pass计数不会增加导致else部分的执行 //无论你是点击取消还是升级按钮。所以你需要提供一些额外的逻辑来 //跟踪它。

于 2013-01-03T04:16:53.213 回答
3

尝试这个 ,

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

}

谢谢

于 2013-03-02T13:46:32.983 回答
2

Karthikeyan答案的Swift.4 版本。

不要忘记为您的 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)
    }
}
于 2019-03-27T07:45:50.590 回答
0

斯威夫特 5.2

通行证的 modifiedDate 将在添加或再次添加时更新您可以获取 modifiedDate 然后将其与当前日期进行比较

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
}
于 2021-06-29T06:28:43.207 回答