Skip to content

Commit e2c1d46

Browse files
committed
Set preferred camera postition for CardIOPaymentViewController.
1 parent 4e9afce commit e2c1d46

11 files changed

+51
-5
lines changed

CardIO_Public_API/CardIOPaymentViewController.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
#import <UIKit/UIKit.h>
77
#import "CardIOPaymentViewControllerDelegate.h"
88
#import "CardIODetectionMode.h"
9+
#import <AVFoundation/AVCaptureDevice.h>
910

1011
/// CardIOPaymentViewController is one of two main entry points into the card.io SDK.
1112
/// @see CardIOView
1213
@interface CardIOPaymentViewController : UINavigationController
1314

15+
- (id)initWithPaymentDelegate:(id<CardIOPaymentViewControllerDelegate>)aDelegate scanningEnabled:(BOOL)scanningEnabled preferredDevicePosition:(AVCaptureDevicePosition)preferredDevicePosition;
16+
1417
/// Initializer for scanning.
1518
/// If scanning is not supported by the user's device, card.io will offer manual entry.
1619
/// @param aDelegate Your CardIOPaymentViewControllerDelegate (typically a UIViewController).

CardIO_Public_API/CardIOView.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#import <UIKit/UIKit.h>
77
#import "CardIOViewDelegate.h"
88
#import "CardIODetectionMode.h"
9+
#import <AVFoundation/AVCaptureDevice.h>
910

1011
/// CardIOView is one of two main entry points into the card.io SDK.
1112
/// @see CardIOPaymentViewController
@@ -20,6 +21,8 @@
2021

2122
#pragma mark - Properties you MAY set
2223

24+
@property(nonatomic, assign, readwrite) AVCaptureDevicePosition preferredDevicePosition;
25+
2326
/// The preferred language for all strings appearing in the user interface.
2427
/// If not set, or if set to nil, defaults to the device's current language setting.
2528
///

Classes/CardIOCameraView.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ - (id)initWithFrame:(CGRect)frame delegate:(id<CardIOVideoStreamDelegate>)delega
8484
_delegate = delegate;
8585
_config = config;
8686

87-
_videoStream = [[CardIOVideoStream alloc] init];
87+
_videoStream = [[CardIOVideoStream alloc] initWithCameraPosition:config.preferredDevicePosition];
8888
self.videoStream.config = config;
8989
self.videoStream.delegate = self;
9090
self.videoStream.previewLayer.needsDisplayOnBoundsChange = YES;
@@ -566,4 +566,4 @@ - (void)setInstructionsFont:(UIFont *)instructionsFont {
566566

567567
@end
568568

569-
#endif
569+
#endif

Classes/CardIOConfig.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#import <Foundation/Foundation.h>
77
#import "CardIOAnalytics.h"
88
#import "CardIODetectionMode.h"
9+
#import <AVFoundation/AVCaptureDevice.h>
910

1011
@interface CardIOConfig : NSObject
1112
@property(nonatomic, strong, readwrite) CardIOAnalytics *scanReport;
@@ -19,6 +20,7 @@
1920
@property(nonatomic, copy, readwrite) NSString *scanInstructions;
2021
@property(nonatomic, assign, readwrite) BOOL hideCardIOLogo;
2122
@property(nonatomic, retain, readwrite) UIView *scanOverlayView;
23+
@property(nonatomic, assign, readwrite) AVCaptureDevicePosition preferredDevicePosition;
2224

2325
@property(nonatomic, assign, readwrite) CardIODetectionMode detectionMode;
2426
@end

Classes/CardIOPaymentViewController.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ - (id)initWithPaymentDelegate:(id<CardIOPaymentViewControllerDelegate>)aDelegate
4747
return [self initWithPaymentDelegate:aDelegate scanningEnabled:YES];
4848
}
4949

50+
- (id)initWithPaymentDelegate:(id<CardIOPaymentViewControllerDelegate>)aDelegate scanningEnabled:(BOOL)scanningEnabled preferredDevicePosition:(AVCaptureDevicePosition)preferredDevicePosition {
51+
_preferredDevicePosition = preferredDevicePosition;
52+
return [self initWithPaymentDelegate:aDelegate scanningEnabled:scanningEnabled];
53+
}
54+
5055
- (id)initWithPaymentDelegate:(id<CardIOPaymentViewControllerDelegate>)aDelegate scanningEnabled:(BOOL)scanningEnabled {
5156
#if CARDIO_DEBUG
5257
static dispatch_once_t onceToken;
@@ -82,6 +87,7 @@ - (id)initWithPaymentDelegate:(id<CardIOPaymentViewControllerDelegate>)aDelegate
8287
#if USE_CAMERA || SIMULATE_CAMERA
8388
if(!self.currentViewControllerIsDataEntry) {
8489
CardIOViewController *cameraVC = (CardIOViewController *)viewController;
90+
cameraVC.preferredDevicePosition = self.preferredDevicePosition;
8591
cameraVC.context = self.context;
8692
}
8793
#endif

Classes/CardIOPaymentViewControllerContinuation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//
55

66
#import "CardIOPaymentViewController.h"
7+
#import <AVFoundation/AVCaptureDevice.h>
78

89
@class CardIOAnalytics;
910
@class CardIOContext;
@@ -22,6 +23,7 @@
2223
@property(nonatomic, assign, readwrite) BOOL statusBarWasOriginallyHidden;
2324

2425
@property(nonatomic, strong, readwrite) CardIOContext *context;
26+
@property(nonatomic, assign, readwrite) AVCaptureDevicePosition preferredDevicePosition;
2527

2628
@property(nonatomic, strong, readwrite) UIImageView *obfuscatingView;
2729

Classes/CardIOVideoStream.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
dmz_context *dmz;
2424
#endif
2525
}
26+
27+
- (id)initWithCameraPosition:(AVCaptureDevicePosition )cameraPosition;
2628
- (void)willAppear;
2729
- (void)willDisappear;
2830

Classes/CardIOVideoStream.mm

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,14 +179,39 @@ @interface CardIOVideoStream ()
179179

180180
@implementation CardIOVideoStream
181181

182+
- (AVCaptureDevice *)cameraWithPosition: (AVCaptureDevicePosition)position {
183+
return [AVCaptureDeviceDiscoverySession discoverySessionWithDeviceTypes:@[ AVCaptureDeviceTypeBuiltInWideAngleCamera ]
184+
mediaType:AVMediaTypeVideo
185+
position:position].devices.firstObject;
186+
}
187+
182188
- (id)init {
183189
if((self = [super init])) {
184190
_interfaceOrientation = (UIInterfaceOrientation)UIDeviceOrientationUnknown;
185191
_scanner = [[CardIOCardScanner alloc] init];
186192
_cameraConfigurationSemaphore = dispatch_semaphore_create(1); // parameter of `1` implies "allow access to only one thread at a time"
187193
#if USE_CAMERA
188194
_captureSession = [[AVCaptureSession alloc] init];
189-
_camera = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
195+
_camera = [self cameraWithPosition: AVCaptureDevicePositionUnspecified];
196+
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
197+
dmz = dmz_context_create();
198+
#elif SIMULATE_CAMERA
199+
_previewLayer = [SimulatedCameraLayer layer];
200+
#endif
201+
}
202+
return self;
203+
}
204+
205+
206+
- (id)initWithCameraPosition:(AVCaptureDevicePosition )cameraPosition {
207+
if((self = [super init])) {
208+
209+
_interfaceOrientation = (UIInterfaceOrientation)UIDeviceOrientationUnknown;
210+
_scanner = [[CardIOCardScanner alloc] init];
211+
_cameraConfigurationSemaphore = dispatch_semaphore_create(1); // parameter of `1` implies "allow access to only one thread at a time"
212+
#if USE_CAMERA
213+
_captureSession = [[AVCaptureSession alloc] init];
214+
_camera = [self cameraWithPosition: cameraPosition];
190215
_previewLayer = [AVCaptureVideoPreviewLayer layerWithSession:self.captureSession];
191216
dmz = dmz_context_create();
192217
#elif SIMULATE_CAMERA

Classes/CardIOView.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ - (void)implicitStart {
148148
return;
149149
}
150150

151+
self.config.preferredDevicePosition = _preferredDevicePosition;
151152
self.scanHasBeenStarted = YES;
152153

153154
CardIOLog(@"Creating cameraView");
@@ -354,4 +355,4 @@ @implementation CardIOView
354355

355356
@end
356357

357-
#endif // USE_CAMERA || SIMULATE_CAMERA
358+
#endif // USE_CAMERA || SIMULATE_CAMERA

Classes/CardIOViewController.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#import <UIKit/UIKit.h>
99

1010
#import <AudioToolbox/AudioServices.h>
11+
#import <AVFoundation/AVCaptureDevice.h>
1112

1213
@class CardIOContext;
1314

@@ -16,6 +17,7 @@
1617
- (id)init;
1718

1819
@property(nonatomic, strong, readwrite) CardIOContext *context;
20+
@property(nonatomic, assign, readwrite) AVCaptureDevicePosition preferredDevicePosition;
1921

2022
@end
2123

0 commit comments

Comments
 (0)