Friday, January 22, 2010

Drupal for iPhone app Update - Upload Images to Drupal from iPhone

This one took a while to figure out...

Step 1) Create the File.Save Service
Drupal will accept your image encoded as base64 and dump into an actual file.
http://drupal.org/node/376226

Step 2) The iPhone will take a picture and then encode it as base64. I figured out that although the CocoaXMLRPC library will send the XMLRPC response with < base64 > tags, drupal doesn't know what to do with the info in those tags. This created a lot of 0 byte files.
The solution is to comment out the < base64 > tag in the CocoaXMLRPC and substitute < string > tag.

If everything works, you'll get a File ID (fid) back.

So construct your XMLRPC Request like you would with a text content type. Then set your Imagedata as one of the PostParams with the forkey 'file' and the method file.save.

Drupal for iPhone app Update - NSSCanner no more

I figured out that the Cocoa XMLRPC library was already doing the work of parsing out the XMLRPC responses. Basically after you get the XMLRPC response, create a NSDictionary and retrieve the key/value pair. Here's some sample code

//So you could put this in the - (void)viewDidLoad
//URL
NSString *drupalurl = @"http://www.drupalwebsite.com/services/xmlrpc";

//XMLRPC request
XMLRPCRequest *reqUserInfo = [[XMLRPCRequest alloc] initWithHost:[NSURL URLWithString:drupalurl]];
[reqUserInfo setMethod:@"system.connect" withObjects:[NSArray arrayWithObjects: nil]];
XMLRPCResponse *Response = [XMLRPCConnection sendSynchronousXMLRPCRequest:reqUserInfo];

//Whole XMLRPC response dumped into dictionary.
NSMutableDictionary *dict = [Response object];

//Anonymous sessid retrieved from the dictionary
NSString *anonsessid = [dict objectForKey:@"sessid"];

VOILA!!