Skip to content

Commit aefcb76

Browse files
committed
Check iOS 8 camera access
1 parent c3aa980 commit aefcb76

1 file changed

Lines changed: 22 additions & 11 deletions

File tree

BitStore/Controller/Scan/ScanViewController.m

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <AVFoundation/AVFoundation.h>
1111
#import "ScanDelegate.h"
1212
#import "URI.h"
13+
#import "UIBAlertView.h"
1314

1415
@interface ScanViewController () <AVCaptureMetadataOutputObjectsDelegate>
1516
@end
@@ -33,19 +34,29 @@ - (void)viewDidLoad {
3334
AVCaptureDeviceInput* videoInput = [AVCaptureDeviceInput deviceInputWithDevice:videoCaptureDevice error:&error];
3435
if (videoInput) {
3536
[_captureSession addInput:videoInput];
37+
AVCaptureMetadataOutput* metadataOutput = [[AVCaptureMetadataOutput alloc] init];
38+
[_captureSession addOutput:metadataOutput];
39+
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
40+
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
41+
42+
AVCaptureVideoPreviewLayer* previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
43+
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
44+
previewLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
45+
[self.view.layer addSublayer:previewLayer];
3646
} else {
37-
NSLog(@"Error: %@", error);
47+
// camera access failed
48+
UIBAlertView* av = [[UIBAlertView alloc] initWithTitle:@"Camera error" message:@"Unable to access the camera. Please enable camera access for BitStore inside the privacy settings." cancelButtonTitle:l10n(@"okay") otherButtonTitles:@"Open Settings", nil];
49+
[av showWithDismissHandler:^(NSInteger selectedIndex, NSString *selectedTitle, BOOL didCancel) {
50+
if (!didCancel) {
51+
BOOL canOpenSettings = (&UIApplicationOpenSettingsURLString != NULL);
52+
if (canOpenSettings) {
53+
NSURL* url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
54+
[[UIApplication sharedApplication] openURL:url];
55+
}
56+
}
57+
}];
58+
[self performSelector:@selector(close:) withObject:self afterDelay:0.01];
3859
}
39-
40-
AVCaptureMetadataOutput* metadataOutput = [[AVCaptureMetadataOutput alloc] init];
41-
[_captureSession addOutput:metadataOutput];
42-
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
43-
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
44-
45-
AVCaptureVideoPreviewLayer* previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
46-
previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
47-
previewLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
48-
[self.view.layer addSublayer:previewLayer];
4960
} else {
5061
self.view.backgroundColor = [UIColor darkGrayColor];
5162
}

0 commit comments

Comments
 (0)