23.9. Interacting with Passbook Programmatically

Problem

You want to be able to interact with the installed passes on a user’s device programmatically.

Solution

Include the PassKit.framework into your project and use the PKPassLibrary to find the passes that you are interested in. Passes will be of type PKPass, so using this class you can retrieve information about your passes.

Discussion

Note

As a prerequisite to this recipe, please make sure that you have read Recipe 23.8 and now have an Xcode iOS project that has the appropriate provisioning profile to access your passes in the user’s Passbook library.

Apple has provided the PassKit.framework for iOS developers. Using this framework, you can interact with passes that the user has installed on her devices. To be able to use this framework using the latest LLVM compiler, all you have to do is import the relevant umbrella header into your project like so:

#import "AppDelegate.h"
#import <PassKit/PassKit.h>

<# Rest of your code goes here #>

The next thing that we are going to do is declare a private property of type PKPassLibrary in the implementation file of our app delegate. The aforementioned class in PassKit.framework will allow you to interact with the passes that have been added to the device. While you are at it, you will also need to know the keys in the pass.json file that you created in Recipe 23.2 in order to be able to read values such as the departure platform and departure city. So declare these keys as well, all in the implementation ...

Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.