Going Up
For the purpose to storing data from an app to the clouds, we would want to convert an NSObject (containing all the stuffs you want to save) into NSData via NSKeyedArchiver. Then perform conversion into NSString using Base64 encoding.
With this data string you can send it up to the database via http POST and store it as string.
//Base64 Encoder/Decoder from LXSupport
NSString *dataStr = [LXSHelperOfNSData stringByEncodeUsingBase64WithData:data];
//Setting up a POST request
NSMutableString *urlString = [[NSMutableString alloc] initWithFormat:@"func=update&uid=uc99&name=dataTester&savefile="];
[urlString appendFormat:@"%@",dataStr];
NSData *postData = [urlString dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *baseurl = @"http://localhost:8888/index.php";
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:postData];
NSError * e;
NSError *error;
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
//Performing the connection
[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
{
if ([data length] > 0 && error == nil)
{
NSLog(@"DATA RECEIVED");
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:data
options:kNilOptions
error:&error];
NSLog(@"Started!: %@",json);
}else if ([data length] == 0 && error == nil)
NSLog(@"DATA EMPTY");
else if (error != nil && error.code != nil)
NSLog(@"DATA TIMEOUT: %d",error.code);
else if (error != nil)
NSLog(@"DATA ERROR");
}];
And Back Down
When reading it back, just perform the reverse actions. Access via http to read the data string from the db, then decode the data and you can serialize it back into the original object for use.
NSData *rdata = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&e];
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:rdata
options:kNilOptions
error:&error];
NSString* dataStr = [json valueForKey:@"savefile"];
NSData* qdata=[dataStr dataUsingEncoding: [NSString defaultCStringEncoding] ];
NSData * data = [LXSHelperOfNSData dataByDecodeUsingBase64WithData:[dataStr dataUsingEncoding: [NSString defaultCStringEncoding]]];