1

I am using PassSlot which creates a Pass on the fly that can be added to passbook. I am trying to get it downloaded to the device to allow attaching to an email. Here is what I have so far:

[PassSlot passFromTemplateWithName:@"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
    [PassSlot downloadPass:pass pass:^(PSPass *pass) {
        PKPass *pkpass = [pass performSelector:@selector(pass)];
        NSLog(@"Pass: %@", pkpass);


        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;


        NSArray *toRecipients = [NSArray arrayWithObject:@"friend@example.com"];


        [picker setToRecipients:toRecipients];


        [picker addAttachmentData:pkpass mimeType:@"application/vnd.apple.pkpass" fileName:@"HI"];

        // Fill out the email body text
        NSString *emailBody = \\
        [picker setMessageBody:emailBody isHTML:NO];

        [self presentViewController:picker animated:YES completion:nil];


    }];
}];

The issue is that in the addAttachment part for the email, it throws an error that NSData can't relate to PKPass basically. How can I get pass converted to NSData so I can attach it?

UPDATE:

I tried doing

 NSURL *url = pkpass.passURL;
 NSData *so = [NSData dataWithContentsOfURL:url];

and then putting 'so' as the addAttachment, but it attached nothing to the email.

Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
user717452
  • 33
  • 14
  • 73
  • 149

2 Answers2

3

Firstly, the passURL property of PKPass doesn't quite work the way you think. It is not a URL to the pass itself. It is a URL that opens up the Passbook app and loads up that requested pass.

You can create a PKPass with NSData, but you can't reverse that process. It sounds as if you are trying to get a pass on device, and then e-mail it. That's not allowed - if it was, people could easily copy and distribute passes around (which isn't necessarily a good thing).

If you want to e-mail a user a pass you need to do it server, rather than client side. I'm afraid that what you're trying to do isn't possible using PassKit. Sorry!

lxt
  • 31,146
  • 5
  • 78
  • 83
  • So, using pass slot services, it creates pass combining a template on their server with info from the user. No way to really email that instead of just building on the device then? Kind of kills idea for app I had. – user717452 Jan 27 '13 at 15:45
  • I would suggest that you ask PassSlot to provide an API method that gives you the raw NSData. I'm not familiar with their library, but I assume it's not open source (if it is you could probably do this yourself). – lxt Jan 27 '13 at 15:51
2

Unfortunately the PassKit library does not provide a way to get back the NSData from a PKPass.

We already provide an API call that allows you to get the raw data of a pass. We will extend our PassSlot SDK with a method that allows you to get the NSData without having the manually call this API method.

Update

The new SDK version 0.5 is now released. You can attach the pass with the following code:

[PassSlot passFromTemplateWithName:@"LoveCouponCards" withValues:values pass:^(PSPass *pass) {
    [PassSlot downloadPass:pass pass:^(PSPass *pass) {

        MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;

        [picker setToRecipients:@[@"friend@example.com"]];
        [picker addAttachmentData:pass.data mimeType:@"application/vnd.apple.pkpass" fileName:@"LoveCouponCard.pkpass"];
        [picker setMessageBody:emailBody isHTML:NO];

        [self presentViewController:picker animated:YES completion:nil];
    }];
}];
PassSlot
  • 211
  • 1
  • 3