API Examples - iPhone/iOS

iPhone Upload Example:

Uploading to FrameSocket via the iPhone is only a matter of correctly formulating a POST request. We've included a sample application below that involves selecting a media file, formulating an authenticated upload request to FrameSocket, and processing the response.

Download: FrameSocket_Upload_Example_iPhone.zip

Please make sure that you insert the correct API Key information within FrameSocket_Upload_ExampleViewController.m so that your requests are not rejected.

There are several helper libraries/packages that can help speed up your development with the iPhone:

The most important part of the code to take notice of is the startUpload function. This function formulates a POST request with both field-values and the actual media file. Lastly - you will notice that this function runs the upload asynchronously, allowing the UI to be updated to display the progress as the upload completes.


// Initialize Upload w/ key Field
NSMutableData *postData = [self generateDataFromText:fsKey fieldName:@"key"];

// Add other authorization data.
[postData appendData:[self generateDataFromText:fsSecret fieldName:@"secret"]];
[postData appendData:[self generateDataFromText:[NSString md5:[fsGatekeeper stringByAppendingString:@"upload"]] fieldName:@"sig"]];

// Optionally Add More Fields
[postData appendData:[self generateDataFromText:@"Test iPhone Upload" fieldName:@"title"]];


//Prepare data for file
NSString *boundary = [[NSString alloc] initWithString:@"AaB03x"];

[postData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"media\"; filename=\"%@\"\r\n", uploadFilename] dataUsingEncoding:NSUTF8StringEncoding]];

[postData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];

// Add Media Data
[postData appendData:[NSData dataWithData:uploadData]];

[postData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

// Setup the request:
NSMutableURLRequest *uploadRequest = [[[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.framesocket.com/api/media/upload.php"] cachePolicy: NSURLRequestReloadIgnoringLocalCacheData timeoutInterval: 60 ] autorelease];
[uploadRequest setHTTPMethod:@"POST"];
[uploadRequest setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"];
[uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"];
[uploadRequest setHTTPBody: postData];

// Run our request in the background...
uploadConnection = [[NSURLConnection alloc] initWithRequest:uploadRequest delegate:self startImmediately:NO];
[uploadConnection scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
[uploadConnection start];


Helper Functions

generateDataFromText - Easily add strings to your POST request.


-(NSMutableData *)generateDataFromText:(NSString *)dataText fieldName:(NSString *)fieldName {
	NSString *post = [NSString stringWithFormat:@"--AaB03x\r\nContent-Disposition: form-data; name=\"%@\"\r\n\r\n", fieldName];
    // Get the post header int ASCII format:
    NSData *postHeaderData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    
    // Generate the mutable data variable:
    NSMutableData *postData = [[NSMutableData alloc] initWithLength:[postHeaderData length] ];
    [postData setData:postHeaderData];
    	
	NSData *uploadData = [dataText dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
	
    // Add the text:
    [postData appendData: uploadData];
    
    // Add the closing boundary:
    [postData appendData: [@"\r\n" dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]];
    
    // Return the post data:
    return postData;
}

Generating MD5 Hashes

We've supplied sample code to help formulate MD5 hashes.

iPhone Content Example:

We'll be posting more examples later on, including how to efficiently display content from FrameSocket within your App.

Have a question? We are currently: