How would I generate a Pass on my web server, then download it in Xcode.
On xcode, I am trying to generate a pass then add it to my passbook wallet. Code:
NSString* passFile = @"http://pass.keatonburleson.com/gen.php?name=128keaton";
//3
NSData *passData = [NSData dataWithContentsOfFile:passFile];
//4
NSError* error = nil;
PKPass *newPass = [[PKPass alloc] initWithData:passData
error:&error];
//5
if (error!=nil) {
[[[UIAlertView alloc] initWithTitle:@"Passes error"
message:[error
localizedDescription]
delegate:nil
cancelButtonTitle:@"Ooops"
otherButtonTitles: nil] show];
return;
}
//6
PKAddPassesViewController *addController =
[[PKAddPassesViewController alloc] initWithPass:newPass];
[self presentViewController:addController
animated:YES
completion:nil];
So basically, my error is ‘trace’ and crashes.
Update: Thanks to Caleb, no more trace error!
Unfortunately, i get a ‘Pass isnt valid error!’ 🙁
Latest and updated code:
NSString* passFile = @"http://pass.keatonburleson.com/gen.php?name=128keaton";
NSURL *url = [NSURL URLWithString:passFile];
//3
NSData *passData = [NSData dataWithContentsOfURL:url];
//4
NSError* error = nil;
PKPass *newPass = [[PKPass alloc] initWithData:passData
error:&error];
BG Info:
My webserver contains a PHP script which generates a passkit pass and displays it for download like a PkPass. If you are on a mac or iPhone you can go to the passFile URL and it will display a valid pass. I think my issue is it is trying to download a linked file when it really generates it an automatically downloads it. I might need to edit some server code to not destroy the ‘saved’ pkpass on my webserver. But, I would rather not do that.
You should probably be using
-dataWithContentsOfURL:since that’s what you’re providing. IspassData == nilafter the second line? If yes, that may be your problem.Note: The PKPass docs say that you should check the console for details if you gt an error from
-initWithData:error:, so look there and let us know what you find.Update:
Unless your server is detecting the client and deciding what to send back based on that, it’s not likely that the same URL that displays the pass returns the pass file itself. Look at the contents of the data you receive and see if its a valid pass. If not, you’ll need to adjust your server or use a different URL.