4

I run the code, as outlined below. When I get the file (NSData), following error appears:

"BOM could not extract archive: Couldn't read PKZip signature"

What is happening? Has anyone had this problem, and how can I fix it?

    NSString *url = [res objectForKey:@"url"];

    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];    if (nil != data) {

    //init a pass library
    PKPassLibrary* passLib = [[PKPassLibrary alloc] init];

    NSError *error;

    //init a pass object with the data
    PKPass *pass = [[PKPass alloc] initWithData:data error:&error];

    if(error) {

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK"otherButtonTitles:nil];
    [alertView show];

    }

    //check if pass library contains this pass already
    if([passLib containsPass:pass]) {

        //pass already exists in library, show an error message
        UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Pass Exists" message:@"The pass you are trying to add to Passbook is already present." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alertView show];

    } else {

        //present view controller to add the pass to the library
        PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass];
        [vc setDelegate:(id)self];
        [self presentViewController:vc animated:YES completion:nil];
    }
}
4444
  • 3,541
  • 10
  • 32
  • 43
  • 2
    Sounds like a problem with the .pkpass bundle and not your code. WDoes the .pkpass bundle ingest properly when you access it via Safari, as a mail attachment or with the pass viewer in OSX? What version of the iOS SDK are you using? – PassKit Jul 22 '13 at 15:15

3 Answers3

0

Assuming that you try to use NSData object which comes from connectionDidReceiveData method, this is wrong. You should accumulate NSData objects until connectionDidFinishLoading.

You should do something like this:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    if (self.passData != nil) {
        [self.passData appendData:data];
    }
    else {
        self.passData = [NSMutableData dataWithData:data];
    }
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSError *error;
    PKPass *pass = [[PKPass alloc] initWithData:self.passData error:&error];
    //add pass
    self.passData = nil;
}
Mert Buran
  • 2,989
  • 2
  • 22
  • 34
0

The problem is in the line

//init a pass object with the data
    PKPass *pass = [[PKPass alloc] initWithData:data error:&error];

The downloaded data is corrupted.

Saurabh Shukla
  • 1,368
  • 3
  • 14
  • 26
0

If you are able to add the pass via Email/Safari, then the problem is due to the NSData object.

In my case, the pkpass files base64 String value was recieved on the device which I converted into to NSData object. PKPass object was able to read my pass. Below is the Code I used:

// dictionary contains base64string values of the pkpass file.
for (NSString *key in [dictionary allKeys])
{
    NSError *error;
    NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:[dictionary valueForKey:key] options:0];
    NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];

    PKPass *pass = [[PKPass alloc] initWithData:decodedData error:&error];
    [arrPasses addObject:pass];

}
if ([arrPasses count] > 0)
{
    PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPasses:arrPasses];
    [self presentViewController:vc animated:YES completion:nil];
}
else
{
    NSLog(@"Passes not found");
}

Hope this helps!!

Yash Soni
  • 164
  • 1
  • 4