5

Hi first of all i have to confess i really don't understand how the whole Passbook topic really works. So here's my situation: I have a backend system which creates .pkpass files stores them and creates an URL. When i open this URL in my browser it directly starts to download the pass file. How can i receive or open this file with my ios application?

Thanks in advance.

d3p0nit
  • 442
  • 5
  • 16

3 Answers3

4

You can use webservices to get pass data. Webservices can send your pass data in base64(NSString) format and you decode it to get NSData.

The use the data to initiate PKPass Object.

PKPass *pass = [[PKPass alloc] initWithData:passData error:&err];

Once you get PKPass you can use PKAddPassesViewController to show it inside the app. You can find detailed explanation here.

Note : you can directly download Pass data from the URL using NSUrlConnection and use the downloaded data to create PKPass.

Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • so i can t catch it as a pkpass file, i need to receive a string and then go with the initWithData method? – d3p0nit May 31 '13 at 13:30
  • Every file is a binary whether it is .pkpass or .png. You have to find a way to get file data. If you are using webservice use a base64 encoded string and decode to NSData. if you streaming directly you can get NSData. Once you get the NSData say iOS that it is really a PkPass by initiating the Object. – Vignesh May 31 '13 at 13:36
  • can i use NSData *data = [NSData dataFromBase64String:originalString]; to decode the base64 string? – d3p0nit May 31 '13 at 14:00
1

another way..simpler and can be used in mobile web apps as well. write a simple web service to return this pass download URL then parse the json/xml(depends on type of response) in your app(web or native ios) and then invoke the URL in safari.

from native app call [[UIApplication sharedApplication] openURL:passdownloadURL];

I have used it in my project and it works awesome!!

vikas
  • 488
  • 8
  • 18
  • Yes i did it the same way, just had some issues how i had to send the pass data to open it the proper way....it works now but thanks for the answer – d3p0nit Jun 14 '13 at 12:15
0

If you host the link to the PKPass item on a website you can check that the page content is really a PKPass item by looking at the mimeType property on HTTPURLResponse, which should be "application/vnd.apple.pkpass". Once you confirm that, download the .pkpass data with an URLRequest and pass the data to the PKPass initializer like this:

var error:NSError?
let passBookData = PKPass( data: data, error: &error )

if error != nil
{
    print( "Error opening pkpass file" )
    return
}
let pkPassVC = PKAddPassesViewController( pass: passBookData )
currentVC.present( pkPassVC, animated: true, completion: completion )
spfursich
  • 5,045
  • 3
  • 21
  • 24