4

我运行代码,如下所述。当我得到文件(NSData)时,出现以下错误:

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

怎么了?有没有人遇到过这个问题,我该如何解决?

    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];
    }
}
4

3 回答 3

0

假设您尝试使用来自 connectionDidReceiveData 方法的 NSData 对象,这是错误的。您应该累积 NSData 对象,直到 connectionDidFinishLoading。

你应该这样做:

-(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;
}
于 2014-06-26T12:47:46.463 回答
0

问题出在一线

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

下载的数据已损坏。

于 2015-03-27T07:49:01.117 回答
0

如果您能够通过电子邮件/Safari 添加通行证,则问题出在 NSData 对象上。

就我而言,在我转换为 NSData 对象的设备上收到了 pkpass 文件 base64 字符串值。PKPass 对象能够读取我的通行证。以下是我使用的代码:

// 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");
}

希望这可以帮助!!

于 2015-05-20T08:57:47.763 回答