Skip to content

Instantly share code, notes, and snippets.

@bitwit
Last active December 12, 2015 03:08
Show Gist options
  • Save bitwit/4704514 to your computer and use it in GitHub Desktop.
Save bitwit/4704514 to your computer and use it in GitHub Desktop.
The Objective-C WebViewControllerDelegate from my blog entry An iOS5 Ready Native Web App Template http://www.bitwit.ca/blog/an-ios5-ready-native-web-app-template/
/**
* WebViewController is responsible for all
* messages that can be used in HTML
*/
@class WebViewController;
@interface WebViewControllerDelegate : NSObject <UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, weak) WebViewController *webViewController;
/*
* Test alert from Objective-C
*/
-(void)message:(NSString *)name;
/* On successful picture selection, a base64 image
* is sent to the JS function processImage();
*/
-(void)takeCameraImage;
-(void)takeLibraryImage;
@end
#import "WebViewControllerDelegate.h"
#import "NSData+Base64.h"
#import "WebViewController.h"
@implementation WebViewControllerDelegate
@synthesize webViewController;
-(void)message:(NSString *)name
{
//Showing a basic pop up alert
NSString *message = [NSString stringWithFormat:
@"Your name is %@", name ];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Message From OBJ-C"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
}
-(void)takeCameraImage
{
if ( ![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] )
{
NSString *message = [NSString stringWithString:
@"Your device does not have a camera" ];
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"No camera available"
message:message
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil, nil];
[alert show];
return;
}
// Set the UIImagePicker, set it to theCamer and set self as the delegate
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.delegate = self;
// Present the image picker
[webViewController presentModalViewController:imagePicker animated:YES];
}
-(void)takeLibraryImage
{
// Set the UIImagePicker, set it to theCamer and set self as the delegate
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.delegate = self;
// Present the image picker
[webViewController presentModalViewController:imagePicker animated:YES];
}
#pragma mark - Image Picker
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//Get the Image
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//flatten it to NSData as a JPEG, low quality
NSData *flatImage = UIImageJPEGRepresentation(image, 0.1f);
// convert NSData to a base 64 encoded string
// NSData+Base64 Category provided by Matt Gallagher
// http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html
//
NSString *image64 = [flatImage base64EncodedString];
//process the image in javascript to be added to the page
NSString *js = [NSString stringWithFormat: @"processImage('data:image/jpeg;base64,%@')", image64];
[webViewController.webView stringByEvaluatingJavaScriptFromString:js];
//dismiss the image picker
[picker dismissModalViewControllerAnimated:YES];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
//cancel was hit inside of the camera view
[picker dismissModalViewControllerAnimated:YES];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment