Check if card already added to PassKit library
Asked Answered
C

2

5

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.

Candiot answered 13/7, 2018 at 7:8 Comment(4)
See this thread for a similar answer I have given: #51061332Superpower
@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.Candiot
You can potentially use let library = PKPassLibrary() let passes = library.passes(of: .payment) You need to have the authorization to see these passes though.Superpower
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.Beckwith
G
6

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

Goidelic answered 30/3, 2020 at 4:33 Comment(6)
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.Beckwith
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.Goidelic
@Goidelic where do you get "yourCardAccountIdentifier" value from?Absolve
@Absolve 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.Goidelic
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-"Absolve
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"]Bruch
P
0

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

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

Prytaneum answered 4/11, 2019 at 11:7 Comment(1)
This has to be the most pointless answer :)Femur

© 2022 - 2024 — McMap. All rights reserved.