开发者

iPhone PLIST File Upload coming out Binary

I have an iPhone app built in Objective C that uploads a PLIST file from the iPhone to a server. Most of the time it works perfectly, but sometimes the file looks like this

bplist00Ú .....

I found out that this is apparently a binary PLIST, but I can't figure out why that would happen.

Any thoughts?

NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:fileName];
NSData *data = [[NSData alloc] initWithContentsOfFile:path]; 

// Location ID doesn't seem to be correct here - always 0?
NSMutableString *urlString = [NSMutableString string];
[urlString appendString:[NSString stringWithFormat:@"http://www.website.com/app/iphone/submitjob/upload?jid=%i&mid=%@&lid=%i", delegate.jobInProgress.jobID, delegate.memberID, [[delegate.jobInProgress.plist objectForKey:@"locationID"] intValue]]];

NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];

NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];

[request addValue:contentType forHTTPHeaderField: @"Content-Type"];

NSMutableData *postbody = [NSMutableData data];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@\"\r\n", fileName] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postbody appendData:[NSData dataWithData:data]];
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncod开发者_Python百科ing:NSUTF8StringEncoding]];
[request setHTTPBody:postbody];

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
returnString = [returnString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];


Are you uploading an existing .plist that is in the bundle? Because all .plist in the project are convert to binary format. I would create any .plist that you are uploading to your server from scratch using CFPropertyListWrite and setting the format to kCFPropertyListXMLFormat_v1_0. Then upload the resulting file to your server. That way, you are not depending on the format that it's in the bundle.


I think Xcode often converts the Info.plist file into binary format. It might depend on what sort of build you are doing, e.g. I have a vague memory that signed builds might need a binary PLIST instead of an XML one, I'm not sure though.

FYI, you can use plutil to convert a PLIST to and from binary/XML format:

plutil -convert xml1 <binary plist file>
plutil -convert binary1 <XML plist file>

You can also use PlistBuddy to manipulate PLIST files, using the -x option to write it out as XML. Here's the man page for PlistBuddy:
http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man8/PlistBuddy.8.html


the plist can have three formats, plain text, xml or a binary format.

the binary format has the advantage that it can be much faster processed when loading or saving on the iphone.

I don't think you can control which format is used, depending on what function you use to write, but there are scripts in perl, python or php that can also parse binary plists.

for python look at https://github.com/farcaller/bplist-python

for php look at https://github.com/rodneyrehm/CFPropertyList


There's setting "Property List Output Encoding" in Project/Target Build Settings which controls output format for most plists: binary, xml, same-as-input.

And you can also modify type for particular file. In XCode 3.x under Get Info in context menu. In XCode 4 - left panel "Utilities" - File Inspector - Identity and Type - File Type. Pure XML type should preserve file as is in all situations.

If you generate plists from code you can use NSPropertyListXMLFormat_v1_0 format instead of NSPropertyListBinaryFormat_v1_0 for xml output.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜