d3p0nit
d3p0nit

Reputation: 442

Receiving Passbook's .pkpass from URL without Webview

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.

Upvotes: 5

Views: 4896

Answers (3)

spfursich
spfursich

Reputation: 5405

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 )

Upvotes: 0

vikas
vikas

Reputation: 498

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!!

Upvotes: 1

Vignesh
Vignesh

Reputation: 10251

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.

Upvotes: 4

Related Questions