Examples | Documentation | CanvasPop Photo Printing API

Examples

The following are code samples to illustrate how to integrate Pop-up Store in mobile applications.


iOS / Objective-C Examples

Note: All Objective-C examples are using Automatic Reference Counting (ARC) and do not include any explicit memory deallocation calls.

API Examples are using All Seeing Interactive's ASIHTTPRequest, a commonly used HTTP request framework, and John Engelhart's JSONKit for JSON parsing.

Push API - Preview Image

- (void)sendPreviewImage:(NSData *)previewImage
{
__unsafe_unretained __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"https://store.canvaspop.com/api/push/preview"]];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
// Include auth headers
[request addRequestHeader:@"CP-Authorization" value:@"basic"];
[request addRequestHeader:@"CP-ApiKey" value:@"YOUR_API_KEY"];
// Load the image
[request addData:previewImage withFileName:@"previewImage" andContentType:@"image/jpeg" forKey:@"photo"];
[request setCompletionBlock:^{
int responseCode = [request responseStatusCode];
if(responseCode == 200) {
// Parse the image token from the response
NSString *responseString = [request responseString];
NSDictionary *responseDictionary = [responseString objectFromJSONString];
NSString *token = [responseDictionary valueForKey:@"image_token"];
NSLog(@"Preview image uploaded successfully, received token: %@", token);
// Open the loader
[self performSelectorInBackground:@selector(openLoader:) withObject:token];
}
else
{
NSLog(@"Preview image handshake failed!");
NSLog(@"HTTP Status code: %d", responseCode);
}
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Preview image upload failed!");
NSLog(@"%@", [error localizedFailureReason]);
}];
[request startAsynchronous];
}
view raw sendPreview.m hosted with ❤ by GitHub

Open Loader

- (void)openLoader:(NSString *)token
{
// Replace with actual print width & height values
int printImageWidth = 2500;
int printImageHeight = 2500;
NSString *urlAddress = [NSString stringWithFormat:@"https://store.canvaspop.com/loader/%@/%i/%i", token, printImageWidth, printImageHeight];
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[self.storeWebView loadRequest:requestObj];
}
view raw openLoader.m hosted with ❤ by GitHub

Push API - Print Image

- (void)sendPrintImage:(NSData *)printImage
{
__unsafe_unretained __block ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:@"https://store.canvaspop.com/api/push/print"]];
[request setDefaultResponseEncoding:NSUTF8StringEncoding];
// Include auth headers
[request addRequestHeader:@"CP-Authorization" value:@"basic"];
[request addRequestHeader:@"CP-ApiKey" value:@"YOUR_API_KEY"];
// Include the preview image token
[request addPostValue:[self previewImageToken] forKey:@"token"];
// Include the image
[request addData:printImage withFileName:@"printImage" andContentType:@"image/jpeg" forKey:@"photo"];
[request setCompletionBlock:^{
int responseCode = [request responseStatusCode];
if(responseCode == 200) {
// Parse the message from the response
NSString *responseString = [request responseString];
NSDictionary *responseDictionary = [responseString objectFromJSONString];
NSString *message = [responseDictionary valueForKey:@"message"];
NSLog(@"Print image upload completed successfully");
NSLog(@"Message: %@", message);
}
else
{
NSLog(@"Print image handshake failed!");
NSLog(@"HTTP Status code: %d", responseCode);
}
}];
[request setFailedBlock:^{
NSError *error = [request error];
NSLog(@"Print image upload failed!");
NSLog(@"%@", [error localizedFailureReason]);
}];
[request startAsynchronous];
}
view raw sendPrint.m hosted with ❤ by GitHub

PopUpStore Event handling

-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType
{
if ([request.URL.absoluteString hasPrefix:@"callback:"]) {
// Parse the components of the URL
NSArray *urlComponents = [request.URL.absoluteString componentsSeparatedByString:@":"];
int componentsCount = [urlComponents count];
// Parse the method/selector name
NSString* selectorName = componentsCount > 1 ? (NSString *)[urlComponents objectAtIndex:1] : nil;
// Parse the arguments if present
NSString* jsonArgs = componentsCount > 2 ? (NSString *)[urlComponents objectAtIndex:2] : nil;
jsonArgs = [jsonArgs stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
id parsedObj = [jsonArgs objectFromJSONString];
NSDictionary* jsonDict = [parsedObj isKindOfClass:[NSDictionary class]] ? parsedObj : nil;
// Determine if we have to call the selector with args or not
if([self respondsToSelector:NSSelectorFromString(selectorName)])
[self performSelectorOnMainThread:NSSelectorFromString(selectorName) withObject:nil waitUntilDone:YES];
else if([self respondsToSelector:NSSelectorFromString([NSString stringWithFormat:@"%@:", selectorName])])
[self performSelectorOnMainThread:NSSelectorFromString([NSString stringWithFormat:@"%@:",selectorName]) withObject:jsonDict waitUntilDone:YES];
return NO;
}
return YES;
}
- (void)userCancelStore
{
NSLog(@"User cancelled store");
// Dismiss/hide webview code
}