为了制作样本存折app,在获取.pkpass文件时,NSData为nil

时间:2012-10-11 13:19:14

标签: ios objective-c passbook

NSString *filePath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:@"freehugcoupon.pkpass"];
NSLog(@"Filepath = [%@]",filePath);
NSData *data = [NSData dataWithContentsOfFile:filePath];

NSError *error;
if (data != nil) {
    PKPass *pass = [[PKPass alloc] initWithData:data error:&error];
    NSLog(@"Error = [%@]",[error localizedDescription]);

    if([passLib containsPass:pass]) {

        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];
        [alertView release];

    } else {

        PKAddPassesViewController *vc = [[PKAddPassesViewController alloc] initWithPass:pass];
        vc.delegate = self;
        [self presentViewController:vc animated:YES completion:nil];
        [vc release];
        [pass release];
    }
}

从本地路径获取.pkpass文件中的NSData时,我得到的NSData对象是nil ...

任何人都可以帮助我 提前谢谢。

1 个答案:

答案 0 :(得分:1)

如果您的NSData对象为nil,则表示您的dataWithContentsOfFile:电话无效。检查您的filePath是否有效 - 如果不是,那就是您的问题。