3

I am trying to add a banking card ("payment pass") to Wallet with PKAddPaymentPassRequest.

So the question is, how can I query the pass library to check if my card (or any other card) is already in the library? For example, to show "Apple Pay enabled" checkmark on a card's UI in my app.

Andrey Solovyov
  • 551
  • 7
  • 21
  • See this thread for a similar answer I have given: https://stackoverflow.com/questions/51060832/how-to-call-the-apple-wallet-from-ios-app-using-swift/51196768?noredirect=1#comment89681945_51196768 – Scott Condron Jul 16 '18 at 12:09
  • @ScottCondron Thanks, but your answer regards just showing PaymentKit controller, meanwhile I would be glad to know how to ask PaymentKit (or any other appropriate framework) whether my card is already added to the library. – Andrey Solovyov Aug 01 '18 at 14:28
  • 1
    You can potentially use `let library = PKPassLibrary()` `let passes = library.passes(of: .payment) ` You need to have the authorization to see these passes though. – Scott Condron Aug 01 '18 at 17:32
  • Did you find any solution here, @AndreySolovyov ? I have the same question. Meanwhile `PKPassLibrary().passes()` returns an empty array, as well as `PKPassLibrary().remotePaymentPasses()`. I have my card added manually via Wallet app. – ChooGoo Jan 19 '21 at 17:28

2 Answers2

5

You can create a PKPassLibrary and get a PKPass Array of the passes with the type "payment card". Then loop through the PKPass Array, check by its primary account identifier or card suffix to see if the card is added already, then change your UI accordingly.

In Swift:

let passLibrary = PKPassLibrary.init()
let paymentPasses = passLibrary.passes(of: .payment) // get PKPass array of payment card
for pass in paymentPasses {
    guard let paymentPass = pass.paymentPass else { return }
    // or check by suffix paymentPass.primaryAccountNumberSuffix
    if paymentPass.primaryAccountIdentifier == "yourCardAccountIdentifier" {
        // do something
    }
}

In Objective-C:

PKPassLibrary *passLibrary = [[PKPassLibrary alloc] init];
NSArray<PKPass *> *paymentPasses = [passLibrary passesOfType:PKPassTypePayment];

for (PKPass *pass in paymentPasses) {
    PKPaymentPass * paymentPass = [pass paymentPass];
    if([paymentPass primaryAccountIdentifier] == @"yourCardAccountIdentifier") {
        // do something
    }
}

And don't forget to include the com.apple.developer.payment-pass-provisioning entitlement in your project. Hope this help you ;)

aturan23
  • 4,798
  • 4
  • 28
  • 52
paky
  • 595
  • 1
  • 5
  • 18
  • I have an issue, `passLibrary.passes` returns an empty array, as well as `passLibrary.remotePaymentPasses()`. Can you help me with this issue please? I have my card added manually via Wallet app. – ChooGoo Jan 19 '21 at 17:30
  • passLibrary.passes will only return those passes that is added by you (the card issuer), it won't return all of the passes in the wallet due to security reason. Simply adding card manually via Wallet won't make it possible to scan all card in the Wallet. If you are the card issuer, ensure your PNO or service provider had added corresponding key. – paky Jan 22 '21 at 03:28
  • @paky where do you get "yourCardAccountIdentifier" value from? – Varrry Feb 11 '21 at 22:14
  • @Varrry it is equal to the PAN (Primary account number) of your card, in short, the 14 - 16 digit number on your plastic credit card. But in most of my use case, checking the suffix is enough. – paky Feb 16 '21 at 02:54
  • thanks @paky. it's strange though, primaryAccountIdentifiers which I've seen in real passes didn't look like PAN, they contained digits prefixed with "V-" – Varrry Feb 16 '21 at 07:14
  • 1
    Comparing `NSString` instances in Obj-C should be done using the `isEqualToString` method. The `==` operator won't work, as this will check that the in-memory instances are the same. Hence the if statement should look like the following: `[[paymentPass primaryAccountIdentifier] isEqualToString@"yourCardAccountIdentifier"]` – Tom Spencer Nov 25 '21 at 15:02
1

According to the documentation the PKPassLibrary has a method called containsPass()

check here: https://developer.apple.com/documentation/passkit/pkpasslibrary/1617110-containspass

matryx87
  • 149
  • 7