1

I'm writing a NSData object to a file using this code:

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"];
NSData *data = [NSData dataFromBase64String:cert];
NSLog(@"Data length is %i", [data length]);
[data writeToFile:thePath atomically:YES]; //doesn't matter atomically YES or NO, same result

And it prints out

Data length is 3078

However when I read the retrieved file I'm missing 70 bytes!

NSString *thePath = [[NSBundle mainBundle] pathForResource:@"cert" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
NSLog(@"PKCS12Data length is %i", [PKCS12Data length]);

Which prints out:

PKCS12Data length is 3008

So what is going on here? Is there another way to create the file with all of its data?

4

1 回答 1

1

You can't write into the app bundle as it is read-only. Write your cert.p12 files into the Documents folder. (I'd guess you already have a cert.p12 file in your app bundle and its size is 3008. Did you check the BOOL return from writeToFile:?)

于 2013-09-12T03:48:02.940 回答