Skip to content

Commit 800b749

Browse files
committed
Fixes crash when user refuses camera access on iOS 8
The first time AVCaptureDeviceInput creation will present a dialog to the user requesting camera access. If the user refuses the creation fails. See WWDC 2013 session #610 for details, but note this behaviour does not seem to be enforced on iOS 7 where as it is with iOS 8.
1 parent f7bc50f commit 800b749

File tree

1 file changed

+24
-20
lines changed

1 file changed

+24
-20
lines changed

QReader/QReader/UYLCaptureViewController.m

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ - (AVCaptureSession *)captureSession
6565
{
6666
if (!_captureSession)
6767
{
68-
_captureSession = [[AVCaptureSession alloc] init];
69-
7068
NSError *error = nil;
7169
AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
7270
if (device.isAutoFocusRangeRestrictionSupported)
@@ -78,36 +76,42 @@ - (AVCaptureSession *)captureSession
7876
}
7977
}
8078

79+
// The first time AVCaptureDeviceInput creation will present a dialog to the user
80+
// requesting camera access. If the user refuses the creation fails.
81+
// See WWDC 2013 session #610 for details, but note this behaviour does not seem to
82+
// be enforced on iOS 7 where as it is with iOS 8.
83+
8184
AVCaptureDeviceInput *deviceInput = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
8285
if (deviceInput)
8386
{
87+
_captureSession = [[AVCaptureSession alloc] init];
8488
if ([_captureSession canAddInput:deviceInput])
8589
{
8690
[_captureSession addInput:deviceInput];
8791
}
92+
93+
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
94+
if ([_captureSession canAddOutput:metadataOutput])
95+
{
96+
[_captureSession addOutput:metadataOutput];
97+
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
98+
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
99+
}
100+
101+
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
102+
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
103+
self.previewLayer.frame = self.view.bounds;
104+
[self.view.layer addSublayer:self.previewLayer];
105+
106+
self.targetLayer = [CALayer layer];
107+
self.targetLayer.frame = self.view.bounds;
108+
[self.view.layer addSublayer:self.targetLayer];
109+
88110
}
89111
else
90112
{
91113
NSLog(@"Input Device error: %@",[error localizedDescription]);
92114
}
93-
94-
AVCaptureMetadataOutput *metadataOutput = [[AVCaptureMetadataOutput alloc] init];
95-
if ([_captureSession canAddOutput:metadataOutput])
96-
{
97-
[_captureSession addOutput:metadataOutput];
98-
[metadataOutput setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];
99-
[metadataOutput setMetadataObjectTypes:@[AVMetadataObjectTypeQRCode]];
100-
}
101-
102-
self.previewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:_captureSession];
103-
self.previewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
104-
self.previewLayer.frame = self.view.bounds;
105-
[self.view.layer addSublayer:self.previewLayer];
106-
107-
self.targetLayer = [CALayer layer];
108-
self.targetLayer.frame = self.view.bounds;
109-
[self.view.layer addSublayer:self.targetLayer];
110-
111115
}
112116
return _captureSession;
113117
}

0 commit comments

Comments
 (0)