PDA

View Full Version : Write/Read plist in <data> format




dova
Oct 28, 2009, 10:08 AM
hello,

i would like to create a plist, then write and read <data></data> format for a key in the plist. The rest of keys are in string format.
The following code does what i want but it writes and reads all key in <string></string> instead of data format.
How can i get it to work?
Thanks!

write:

/* write plist */
NSString *plistPath = @"/Users/dova/Desktop/data.plist";
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] init];
[plistDict setObject:info forKey:@"name"];
...
[plistDict writeToFile:plistPath atomically:YES];


read:
/* read plist */
NSString *plistPath = @"/Users/dova/Desktop/data.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSData *value;
value = [plistDict objectForKey:@"name"];



robbieduncan
Oct 28, 2009, 10:14 AM
What type of object is info? If it's a NSString then it'll get written as a string. You would need to convert (not cast) to NSData to get what you want.

dova
Oct 28, 2009, 10:27 AM
The info is a string type (I got it to write and read it).
The next key is idata (setObject:idata forKey:@"data") in the string type, and i would like to convert it to data format for both read and write. but i do not know how.

robbieduncan
Oct 28, 2009, 10:32 AM
Read the documentation (http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/dataUsingEncoding:).

dova
Oct 28, 2009, 10:58 AM
i got it to encode in the data format:
NSData * datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
[plistDict setObject:datax forKey:@"data"];

how is it with decode to read? i do not found any info for decoding in the documentation.
Thank you

robbieduncan
Oct 28, 2009, 11:00 AM
i got it to encode in the data format:
NSData * datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
[plistDict setObject:datax forKey:@"data"];

how it is with decode? i do not found any info for decoding in the documentation.
Thank you

Are you serious? On the same ****** page I linked to above is this, amazingly obviously named, clearly documented (http://developer.apple.com/mac/library/documentation/cocoa/reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/instm/NSString/initWithData:encoding:) init method.

In all seriousness: if you couldn't see that give up now.

dova
Oct 28, 2009, 11:32 AM
Thank you!
i got it to decode:
NSData *datax = [idata dataUsingEncoding:NSUTF8StringEncoding];
NSString *dec_data = [[NSString alloc] initWithData:datax encoding:NSUTF8StringEncoding];
NSLog (@"decode: %@\n", dec_data);

robbieduncan
Oct 28, 2009, 11:57 AM
The real question is why? This is not really any more secure than just saving it as a string...

dova
Oct 28, 2009, 12:32 PM
I use the data format to store the encrypted key using openssl. The key is encrypted as plain text:
2f%•ae˝dbG.oo]!?fi?√π–F?G—Yo@5√;˝?…?8X?ŒkOT

Just to make sure that i won't have problem with some weird characters i need to convert it in the data format:
<data>
MmYlwqzigKJhw4Zly53DrWRiRy5vb13DiiHDkgTDje+sgRriiJrPgOKAk8O5RhxH4oCU
w7ZZb8KsQDXDhOKImsOPw4g7w6TLncOBHcOo4oCmw4HDiQU4w4lYw6ocxZJrw5pPwqHD
rlQATAfDkjx4bDfiiJrDt28Twq9vw6R64oC64omIw7jigJN64oCdw4QfDSrDtTA3B8ub
4oirw6fDssOVw7Q+wqNdZG9wFDx9a1Uzy4ZOw40oPWzCoMuGw4zCriHDiRJtxpLDqQ==
</data>

robbieduncan
Oct 28, 2009, 03:34 PM
Ah, OK, well that sounds sensible. I would check/ensure that the encoding you are choosing can represent all the characters you have though...