Skip to content

Commit 3fda365

Browse files
committed
Handle Camera Privacy Settings Failue
Show user alert view when camera access fails and if supported allow user to directly launch settings app
1 parent 94b964b commit 3fda365

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

QReader/QReader.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
5309FFAE19183E2800ADB15A /* UYLTextCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 5309FFAD19183E2800ADB15A /* UYLTextCell.m */; };
11+
53249C0419AF6D8400EA1639 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 53249C0219AF6D8400EA1639 /* Localizable.strings */; };
1112
532884FD19197D6C00E44C28 /* UYLCaptureViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 532884FC19197D6C00E44C28 /* UYLCaptureViewController.m */; };
1213
5344A59918DF769400C61776 /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5344A59818DF769400C61776 /* Foundation.framework */; };
1314
5344A59B18DF769400C61776 /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5344A59A18DF769400C61776 /* CoreGraphics.framework */; };
@@ -26,6 +27,7 @@
2627
5309FFAC19183E2800ADB15A /* UYLTextCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UYLTextCell.h; sourceTree = "<group>"; };
2728
5309FFAD19183E2800ADB15A /* UYLTextCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UYLTextCell.m; sourceTree = "<group>"; };
2829
53191C0C1922A774000DCCBB /* README */ = {isa = PBXFileReference; lastKnownFileType = text; path = README; sourceTree = "<group>"; };
30+
53249C0319AF6D8400EA1639 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/Localizable.strings; sourceTree = "<group>"; };
2931
532884FB19197D6C00E44C28 /* UYLCaptureViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UYLCaptureViewController.h; sourceTree = "<group>"; };
3032
532884FC19197D6C00E44C28 /* UYLCaptureViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UYLCaptureViewController.m; sourceTree = "<group>"; };
3133
5344A59518DF769400C61776 /* QReader.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = QReader.app; sourceTree = BUILT_PRODUCTS_DIR; };
@@ -135,6 +137,7 @@
135137
5344A5A118DF769400C61776 /* InfoPlist.strings */,
136138
5344A5A418DF769400C61776 /* main.m */,
137139
5344A5A618DF769400C61776 /* QReader-Prefix.pch */,
140+
53249C0219AF6D8400EA1639 /* Localizable.strings */,
138141
);
139142
name = "Supporting Files";
140143
sourceTree = "<group>";
@@ -197,6 +200,7 @@
197200
isa = PBXResourcesBuildPhase;
198201
buildActionMask = 2147483647;
199202
files = (
203+
53249C0419AF6D8400EA1639 /* Localizable.strings in Resources */,
200204
5344A5AF18DF769400C61776 /* Main_iPad.storyboard in Resources */,
201205
5344A5B418DF769400C61776 /* Images.xcassets in Resources */,
202206
5344A5AC18DF769400C61776 /* Main_iPhone.storyboard in Resources */,
@@ -223,6 +227,14 @@
223227
/* End PBXSourcesBuildPhase section */
224228

225229
/* Begin PBXVariantGroup section */
230+
53249C0219AF6D8400EA1639 /* Localizable.strings */ = {
231+
isa = PBXVariantGroup;
232+
children = (
233+
53249C0319AF6D8400EA1639 /* en */,
234+
);
235+
name = Localizable.strings;
236+
sourceTree = "<group>";
237+
};
226238
5344A5A118DF769400C61776 /* InfoPlist.strings */ = {
227239
isa = PBXVariantGroup;
228240
children = (

QReader/QReader/QReader-Info.plist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
<string>1.0</string>
2525
<key>LSRequiresIPhoneOS</key>
2626
<true/>
27+
<key>NSCameraUsageDescription</key>
28+
<string>The camera is used to capture QR codes.</string>
2729
<key>UIMainStoryboardFile</key>
2830
<string>Main_iPhone</string>
2931
<key>UIMainStoryboardFile~ipad</key>

QReader/QReader/UYLCaptureViewController.m

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#import "UYLCaptureViewController.h"
3636
#import "UYLTableViewController.h"
3737

38-
@interface UYLCaptureViewController () <AVCaptureMetadataOutputObjectsDelegate>
38+
@interface UYLCaptureViewController () <AVCaptureMetadataOutputObjectsDelegate, UIAlertViewDelegate>
3939

4040
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *previewLayer;
4141
@property (nonatomic, strong) CALayer *targetLayer;
@@ -111,6 +111,7 @@ - (AVCaptureSession *)captureSession
111111
else
112112
{
113113
NSLog(@"Input Device error: %@",[error localizedDescription]);
114+
[self showAlertForCameraError:error];
114115
}
115116
}
116117
return _captureSession;
@@ -197,10 +198,44 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:
197198
[self showDetectedObjects];
198199
}
199200

201+
#pragma mark -
202+
#pragma mark === UIAlertViewDelegate ===
203+
#pragma mark -
204+
205+
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
206+
{
207+
if (buttonIndex == 1)
208+
{
209+
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
210+
[[UIApplication sharedApplication] openURL:url];
211+
}
212+
}
213+
200214
#pragma mark -
201215
#pragma mark === Utility methods ===
202216
#pragma mark -
203217

218+
- (void)showAlertForCameraError:(NSError *)error
219+
{
220+
NSString *buttonTitle = nil;
221+
NSString *message = error.localizedFailureReason ? error.localizedFailureReason : error.localizedDescription;
222+
223+
if ((error.code == AVErrorApplicationIsNotAuthorizedToUseDevice) &&
224+
UIApplicationOpenSettingsURLString)
225+
{
226+
// Starting with iOS 8 we can directly open the settings bundle
227+
// for this App so add a settings button to the alert view.
228+
buttonTitle = NSLocalizedString(@"AlertViewSettingsButton", @"Settings");
229+
}
230+
231+
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"AlertViewTitleCameraError", @"Camera Error")
232+
message:message
233+
delegate:self
234+
cancelButtonTitle:NSLocalizedString(@"AlertViewCancelButton", @"Cancel")
235+
otherButtonTitles:buttonTitle, nil];
236+
[alertView show];
237+
}
238+
204239
- (void)startRunning
205240
{
206241
self.codeObjects = nil;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
Localizable.strings
3+
QReader
4+
5+
Created by Keith Harrison on 28/08/2014.
6+
Copyright (c) 2014 Keith Harrison. All rights reserved.
7+
*/
8+
9+
"AlertViewTitleCameraError" = "Camera Error";
10+
"AlertViewCancelButton" = "Cancel";
11+
"AlertViewSettingsButton" = "Settings";

QReader/README

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
=======================================================================
22
QReader - Reading QR Codes
3+
Version 1.1 28 Aug 2014 Add handling of camera privacys settings.
34
Version 1.0 12 May 2014 Initial version.
45
=======================================================================
56

67
Example of how to use AVFoundation to capture QR codes. See the following post for details:
78

8-
http://useyourloaf.com/blog/2014/05/13/reading-qr-codes.html
9+
http://useyourloaf.com/blog/2014/05/13/reading-qr-codes.html
10+
11+
For details on adding an alert view to warn the user when the camera cannot be
12+
accessed and directly launch the settings (requires iOS 8) see the following post:
13+
14+
http://useyourloaf.com/blog/2014/08/28/open-settings-url.html

0 commit comments

Comments
 (0)