mirror of
https://gitee.com/shuto/cordova-plugin-camera.git
synced 2025-05-08 03:02:53 +08:00
CB-7667 iOS8: Handle case where camera is not authorized (closes #49)
In iOS 7+, when the app does not have access to the camera, show a prompt notifying the user so they're not puzzled by looking at a black screen. In iOS 8+, include a link on the dialog to open the Setting app to allow the user to change their Camera privacy setting. Signed-off-by: Shazron Abdullah <shazron@apache.org>
This commit is contained in:
parent
b8b53c6254
commit
9f9e5ef4a9
@ -146,6 +146,7 @@
|
|||||||
<framework src="AssetsLibrary.framework" />
|
<framework src="AssetsLibrary.framework" />
|
||||||
<framework src="MobileCoreServices.framework" />
|
<framework src="MobileCoreServices.framework" />
|
||||||
<framework src="CoreGraphics.framework" />
|
<framework src="CoreGraphics.framework" />
|
||||||
|
<framework src="AVFoundation.framework" />
|
||||||
|
|
||||||
<config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
|
<config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
|
||||||
<string></string>
|
<string></string>
|
||||||
|
@ -24,6 +24,7 @@
|
|||||||
#import <ImageIO/CGImageProperties.h>
|
#import <ImageIO/CGImageProperties.h>
|
||||||
#import <AssetsLibrary/ALAssetRepresentation.h>
|
#import <AssetsLibrary/ALAssetRepresentation.h>
|
||||||
#import <AssetsLibrary/AssetsLibrary.h>
|
#import <AssetsLibrary/AssetsLibrary.h>
|
||||||
|
#import <AVFoundation/AVFoundation.h>
|
||||||
#import <ImageIO/CGImageSource.h>
|
#import <ImageIO/CGImageSource.h>
|
||||||
#import <ImageIO/CGImageProperties.h>
|
#import <ImageIO/CGImageProperties.h>
|
||||||
#import <ImageIO/CGImageDestination.h>
|
#import <ImageIO/CGImageDestination.h>
|
||||||
@ -141,7 +142,29 @@ static NSString* toBase64(NSData* data) {
|
|||||||
[weakSelf.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
[weakSelf.commandDelegate sendPluginResult:result callbackId:command.callbackId];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Validate the app has permission to access the camera
|
||||||
|
if (pictureOptions.sourceType == UIImagePickerControllerSourceTypeCamera && [AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)]) {
|
||||||
|
AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
|
||||||
|
if (authStatus == AVAuthorizationStatusDenied ||
|
||||||
|
authStatus == AVAuthorizationStatusRestricted) {
|
||||||
|
// If iOS 8+, offer a link to the Settings app
|
||||||
|
NSString* settingsButton = (&UIApplicationOpenSettingsURLString != NULL)
|
||||||
|
? NSLocalizedString(@"Settings", nil)
|
||||||
|
: nil;
|
||||||
|
|
||||||
|
// Denied; show an alert
|
||||||
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
|
[[[UIAlertView alloc] initWithTitle:[[NSBundle mainBundle]
|
||||||
|
objectForInfoDictionaryKey:@"CFBundleDisplayName"]
|
||||||
|
message:NSLocalizedString(@"Access to the camera has been prohibited; please enable it in the Settings app to continue.", nil)
|
||||||
|
delegate:self
|
||||||
|
cancelButtonTitle:NSLocalizedString(@"OK", nil)
|
||||||
|
otherButtonTitles:settingsButton, nil] show];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CDVCameraPicker* cameraPicker = [CDVCameraPicker createFromPictureOptions:pictureOptions];
|
CDVCameraPicker* cameraPicker = [CDVCameraPicker createFromPictureOptions:pictureOptions];
|
||||||
weakSelf.pickerController = cameraPicker;
|
weakSelf.pickerController = cameraPicker;
|
||||||
|
|
||||||
@ -174,6 +197,25 @@ static NSString* toBase64(NSData* data) {
|
|||||||
}];
|
}];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Delegate for camera permission UIAlertView
|
||||||
|
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
|
||||||
|
{
|
||||||
|
// If Settings button (on iOS 8), open the settings app
|
||||||
|
if (buttonIndex == 1) {
|
||||||
|
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dismiss the view
|
||||||
|
[[self.pickerController presentingViewController] dismissViewControllerAnimated:YES completion:nil];
|
||||||
|
|
||||||
|
CDVPluginResult* result = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"has no access to camera"]; // error callback expects string ATM
|
||||||
|
|
||||||
|
[self.commandDelegate sendPluginResult:result callbackId:self.pickerController.callbackId];
|
||||||
|
|
||||||
|
self.hasPendingOperation = NO;
|
||||||
|
self.pickerController = nil;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)repositionPopover:(CDVInvokedUrlCommand*)command
|
- (void)repositionPopover:(CDVInvokedUrlCommand*)command
|
||||||
{
|
{
|
||||||
NSDictionary* options = [command argumentAtIndex:0 withDefault:nil];
|
NSDictionary* options = [command argumentAtIndex:0 withDefault:nil];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user