3

I created a pass statically by typing command line in order to create pass.pkpass. I also use web service in MAMP. How to generate a pass dynamically ? and where is pass.pkpass stored ?

malinchhan
  • 767
  • 2
  • 8
  • 28

1 Answers1

3

To generate the pass dynamically, you will need code that will:

  • Retrieve all the dynamic data that you want to put into the pass
  • Create a pass.json file containing the dynamic pass data and compute its SHA1 hash
  • Gather the pass assets (images, locale strings, etc.) and compute their SHA1 hash
  • Assemble the manifest file (list of files in the .pkpass bundle and their SHA1 hashes)
  • Sign the manifest file with your PassID Certificate and include the Apple WWDR cert
  • Zip all of the assets up into a .pkpass bundle
  • Serve the .pkpass bundle with the correct MIME type
  • Cleanup any temporary folders/files you use in the above process

You may also need code to:

  • Generate random tokens for the serialNumber and authenticationToken
  • Record the pass details in a database

As for where the .pkpass files go, this is entirely up to you. You can either store them or delete them once they have been served to the device. Since the code that you will need can recreate the .pkpass bundle, and since your web service will send a 304 response to a device that already has the latest version of the pass, there is little value in keeping these files on your server.

If you are using MAMP, then this PHP Library is a good place to start.

PassKit
  • 12,231
  • 5
  • 57
  • 75
  • So I need to write a pass.json ? and how to compute SHA1 hash ? – malinchhan Apr 20 '13 at 03:28
  • [Using PHP you could use](http://www.php.net/manual/en/function.sha1-file.php) `sha1_file('pass.json');`. In the [library above](https://github.com/tschoffelen/PHP-Passkit/blob/master/PKPass/PKPass.php), the code to build and sign the manifest is starts on line 258 – PassKit Apr 20 '13 at 03:33
  • why I have to compute SHA1 hash ? – malinchhan Apr 22 '13 at 06:06
  • Because [the specification](https://developer.apple.com/library/ios/#documentation/UserExperience/Reference/PassKit_Bundle/Chapters/PackageStructure.html#//apple_ref/doc/uid/TP40012026-CH1-SW1) requires that manifest.json contains a list of every file in the .pkpass bundle together with it's SHA1 hash. Why don't you just use the PHP library since that does absolutely everything you need to dynamically generate a pass. It helps you generate the pass.json then does the same as the signpass command line tool but using PHP. – PassKit Apr 22 '13 at 06:19
  • When I test full_sample, it works ! but when I change to my pass, an alert appear: "safari cannot download this file " . How can I solve this problem ? – malinchhan Apr 23 '13 at 03:55
  • Have a look at the console in Xcode when you try to add the pass to the phone to see why it is not adding. – PassKit Apr 23 '13 at 03:59
  • If I want to store user info to database, should I add at the same time that device register pass ? but how ? – malinchhan Apr 23 '13 at 04:23
  • Not sure what you mean by "user info". The best time to collect data and link it to a pass record is when you first issue it. [This pass](http://fb-pass.com/abba-java-campaign/7fUGEqe95va4cE9eeP3pSW) for example gather's info from a user's Facebook account and then uses it to create a pass. When the user connects via Facebook, we take the data and store it together with the pass record. The problem with trying to do this when the device registers, is that a needs to have been created before it can register, and you may need that user information before you can create the pass. – PassKit Apr 23 '13 at 04:46
  • If user clicks on create pass button from my local server, I have to create a pass and save user's name, id, etc to my database, but if user click cancel button on the pass, so it is not right to have user's data. Then, I think if I know that user clicks on add button, then I will add user's info to database. By my question before, you answer me that I cannot detect if user clicks on add or cancel button. So, do you have some other comments ? – malinchhan Apr 23 '13 at 09:35
  • Here's the problem - the button click and the add/cancel click are completely independent - the 'pass' has no way of knowing that it came from the button, and it cannot 'tell' your server anything about the itself, other than the `passTypeIdentifier` and `serialNumber`. So when your device registers, you need some way of matching the `serialNumber` and `passTypeIdentifier` to the user details, otherwise you have no way of getting them. You could always have a status field in your database that changes to 'registered' then periodically delete pass records that were never added. – PassKit Apr 23 '13 at 10:01
  • now in table device, I add userID as foreign key from table user, can I do like this ? – malinchhan Apr 23 '13 at 10:14
  • I would probably put it in the pass table rather than the device table. Because when the device registers it queries the pass table, not the device table. – PassKit Apr 23 '13 at 10:21
  • May be that ! as the pass will be added by many users, I will create another table to store userID and primary key of pass ! but my server has only 1 pass, do u think it is useful to have another table ? – malinchhan Apr 23 '13 at 10:27
  • I mean I generate only 1 type of pass as generic , only different the front content ! – malinchhan Apr 23 '13 at 10:28