Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

scanf

macrumors newbie
Original poster
Apr 23, 2010
15
0
i have some application-specific data that i want to encrypt and store in a file (before i've compiled the app - not at runtime) as a resource in my app bundle. the problem is that my encryption algorithm returns an NSData object that looks like <5956b4f7 b8aecea1 633f6178 18368c78> and if i paste that manually into that file and subsequently read it using stringWithContentsOfFile and convert that string back to NSData to decrypt it, it doesn't work.

bottom line, how can i manually store an NSData object so it can be read later?
 
yeah, i tried that and it kept causing this exception:

-[NSConcreteData writeToFile:atomically:encoding:error:]: unrecognized selector sent to instance
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSConcreteData writeToFile:atomically:encoding:error:]: unrecognized selector sent to instance 0x5a1e480'

Code:
NSError *error = nil;
NSString* filePath = [[NSBundle mainBundle] pathForResource:@"temp" ofType:@"txt"];
[encryptedData writeToFile:filePath atomically:NO encoding:NSUTF8StringEncoding error:&error];

i'm also getting strange compiler warnings:
'NSData' may not respond to '-writeToFile:atomically:encodeing:error:'
Initialization makes integer from pointer without cast
 
No, you tried to use -writeToFile:atomically:encodeing:error:. This is a NSData method you have invented. Look at the NSData documentation (I linked to it in my previous post). Do you see that method? No, bit you do see -writeToFile:atomically: which is what I suggest you use before and what I suggest you use now.
 
oops, i hurredly copied that from a different part of my code where i first tried converting the NSData output to a string then writing it.

anyway, it compiles and runs fine and writeToFile returns YES, but the file is not being written to for some reason. any extra step i need to take to write to the file? i can read from them just fine.

many thanks for your help and quick responses!
 
ahhhhh, ok - this is strange. here's the path it's writing to:
/Users/me/Library/Application Support/iPhone Simulator/4.0/Applications/5F1F25E5-35D8-484E-8853-1589F8E1AB3F/testApp.app/temp.txt

not sure what that location is for as opposed to the app in my build directory. i was expecting to see my project's temp.txt file updated. is that possible?
 
You can't write to your app's bundle. It's read-only. Write to the Documents folder or elsewhere inside your app's sandbox.

However, on the Sim a write to the app's bundle will succeed but on the device it will fail.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.