diff --git a/.gitignore b/.gitignore index 4f26f36..10ed8ea 100644 --- a/.gitignore +++ b/.gitignore @@ -20,4 +20,5 @@ CordovaLib/javascript/cordova-*.js wkhtmltopdf.dmg wkhtmltopdf/ -CordovaLib/CordovaLibApp/www/cordova.ios.js \ No newline at end of file +CordovaLib/CordovaLibApp/www/cordova.ios.js +/.project diff --git a/CordovaLib/Classes/CDVCapture.h b/CordovaLib/Classes/CDVCapture.h index 20b376c..ff7378c 100644 --- a/CordovaLib/Classes/CDVCapture.h +++ b/CordovaLib/Classes/CDVCapture.h @@ -38,17 +38,19 @@ typedef NSUInteger CDVCaptureError; NSString* callbackid; NSInteger quality; NSString* mimeType; + UIPopoverController* popoverController; } @property (assign) NSInteger quality; @property (copy) NSString* callbackId; @property (copy) NSString* mimeType; +@property (assign) UIPopoverController* popoverController; - (void) dealloc; @end -@interface CDVCapture : CDVPlugin +@interface CDVCapture : CDVPlugin { CDVImagePicker* pickerController; BOOL inUse; diff --git a/CordovaLib/Classes/CDVCapture.m b/CordovaLib/Classes/CDVCapture.m index 694aea2..80a733d 100644 --- a/CordovaLib/Classes/CDVCapture.m +++ b/CordovaLib/Classes/CDVCapture.m @@ -33,6 +33,7 @@ @synthesize quality; @synthesize callbackId; @synthesize mimeType; +@synthesize popoverController; - (uint64_t) accessibilityTraits @@ -63,6 +64,12 @@ @implementation CDVCapture @synthesize inUse; +-(BOOL)popoverSupported +{ + return (NSClassFromString(@"UIPopoverController") != nil) + && (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad); +} + -(id)initWithWebView:(UIWebView *)theWebView { self = (CDVCapture*)[super initWithWebView:theWebView]; @@ -112,11 +119,24 @@ NSString* callbackId = [arguments objectAtIndex:0]; NSString* mode = [options objectForKey:@"mode"]; + NSString* sourceTypeString = [options valueForKey:@"sourceType"]; + UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; // default + if( sourceTypeString != nil ) { + int sourceTypeInt = [sourceTypeString intValue]; + if( 0 == sourceTypeInt ) { + sourceType = UIImagePickerControllerSourceTypePhotoLibrary; + } else if( 1 == sourceTypeInt ) { + sourceType = UIImagePickerControllerSourceTypeCamera; + } else { + NSLog(@"Capture.captureImage: sourceType (%@) invalid. Using default.", sourceTypeString); + }; + } + //options could contain limit and mode neither of which are supported at this time // taking more than one picture (limit) is only supported if provide own controls via cameraOverlayView property // can support mode in OS - if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { + if (![UIImagePickerController isSourceTypeAvailable:sourceType]) { NSLog(@"Capture.imageCapture: camera not available."); CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageToErrorObject:CAPTURE_NOT_SUPPORTED]; [self writeJavascript:[result toErrorCallbackString:callbackId]]; @@ -128,7 +148,7 @@ } pickerController.delegate = self; - pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; + pickerController.sourceType = sourceType; pickerController.allowsEditing = NO; if ([pickerController respondsToSelector:@selector(mediaTypes)]) { // iOS 3.0 @@ -143,12 +163,25 @@ // CDVImagePicker specific property pickerController.callbackId = callbackId; pickerController.mimeType = mode; - - if ([self.viewController respondsToSelector:@selector(presentViewController:::)]) { - [self.viewController presentViewController:pickerController animated:YES completion:nil]; + + if( [self popoverSupported] && sourceType != UIImagePickerControllerSourceTypeCamera ) { + if( pickerController.popoverController == nil ) { + pickerController.popoverController = [[NSClassFromString(@"UIPopoverController") alloc] + initWithContentViewController:pickerController]; + } + pickerController.popoverController.delegate = self; + [ pickerController.popoverController + presentPopoverFromRect:CGRectMake(0,32,320,480) + inView:[self.webView superview] + permittedArrowDirections:UIPopoverArrowDirectionAny + animated:YES ]; } else { - [self.viewController presentModalViewController:pickerController animated:YES ]; - } + if ([self.viewController respondsToSelector:@selector(presentViewController:::)]) { + [self.viewController presentViewController:pickerController animated:YES completion:nil]; + } else { + [self.viewController presentModalViewController:pickerController animated:YES ]; + } + } } } @@ -215,7 +248,20 @@ //NSNumber* duration = [options objectForKey:@"duration"]; NSString* mediaType = nil; - if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { + NSString* sourceTypeString = [options valueForKey: @"sourceType"]; + UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera; //default + if( sourceTypeString != nil ) { + int sourceTypeInt = [sourceTypeString intValue]; + if( 0 == sourceTypeInt ) { + sourceType = UIImagePickerControllerSourceTypePhotoLibrary; + } else if( 1 == sourceTypeInt ) { + sourceType = UIImagePickerControllerSourceTypeCamera; + } else { + NSLog(@"Capture.captureVideo: sourceType (%@) invalid. Use default.", sourceTypeString); + }; + } + + if ([UIImagePickerController isSourceTypeAvailable:sourceType]) { // there is a camera, it is available, make sure it can do movies if (pickerController != nil) { [pickerController release]; // create a new one for each instance to initialize all variables @@ -224,7 +270,7 @@ NSArray* types = nil; if ([UIImagePickerController respondsToSelector: @selector(availableMediaTypesForSourceType:)]){ - types = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera]; + types = [UIImagePickerController availableMediaTypesForSourceType:sourceType]; //NSLog(@"MediaTypes: %@", [types description]); if ([types containsObject:(NSString*)kUTTypeMovie]){ @@ -242,7 +288,7 @@ } else { pickerController.delegate = self; - pickerController.sourceType = UIImagePickerControllerSourceTypeCamera; + pickerController.sourceType = sourceType; pickerController.allowsEditing = NO; // iOS 3.0 pickerController.mediaTypes = [NSArray arrayWithObjects: mediaType, nil]; @@ -262,12 +308,25 @@ } // CDVImagePicker specific property pickerController.callbackId = callbackId; - - if ([self.viewController respondsToSelector:@selector(presentViewController:::)]) { - [self.viewController presentViewController:pickerController animated:YES completion:nil]; + + if( [self popoverSupported] && sourceType != UIImagePickerControllerSourceTypeCamera ) { + if( pickerController.popoverController == nil ) { + pickerController.popoverController = [[NSClassFromString(@"UIPopoverController") alloc] + initWithContentViewController:pickerController]; + } + pickerController.popoverController.delegate = self; + [ pickerController.popoverController + presentPopoverFromRect:CGRectMake(0,32,320,480) + inView:[self.webView superview] + permittedArrowDirections:UIPopoverArrowDirectionAny + animated:YES ]; } else { - [self.viewController presentModalViewController:pickerController animated:YES ]; - } + if ([self.viewController respondsToSelector:@selector(presentViewController:::)]) { + [self.viewController presentViewController:pickerController animated:YES completion:nil]; + } else { + [self.viewController presentModalViewController:pickerController animated:YES ]; + } + } } } @@ -484,11 +543,15 @@ CDVImagePicker* cameraPicker = (CDVImagePicker*)picker; NSString* callbackId = cameraPicker.callbackId; - if ([picker respondsToSelector:@selector(presentingViewController)]) { - [[picker presentingViewController] dismissModalViewControllerAnimated:YES]; + if( nil != cameraPicker.popoverController ) { + [cameraPicker.popoverController dismissPopoverAnimated:YES]; } else { - [[picker parentViewController] dismissModalViewControllerAnimated:YES]; - } + if ([picker respondsToSelector:@selector(presentingViewController)]) { + [[picker presentingViewController] dismissModalViewControllerAnimated:YES]; + } else { + [[picker parentViewController] dismissModalViewControllerAnimated:YES]; + } + } NSString* jsString = nil; CDVPluginResult* result = nil; @@ -531,11 +594,15 @@ CDVImagePicker* cameraPicker = (CDVImagePicker*)picker; NSString* callbackId = cameraPicker.callbackId; - if ([picker respondsToSelector:@selector(presentingViewController)]) { - [[picker presentingViewController] dismissModalViewControllerAnimated:YES]; + if( nil != cameraPicker.popoverController ) { + [cameraPicker.popoverController dismissPopoverAnimated:YES]; } else { - [[picker parentViewController] dismissModalViewControllerAnimated:YES]; - } + if ([picker respondsToSelector:@selector(presentingViewController)]) { + [[picker presentingViewController] dismissModalViewControllerAnimated:YES]; + } else { + [[picker parentViewController] dismissModalViewControllerAnimated:YES]; + } + } NSString* jsString = nil; CDVPluginResult* result = nil;