If you only need QR Code scanning, it's much easier to do this with native means:
In the .h of your VC add:
#import <AVFoundation/AVFoundation.h>
@interface FEQRViewController : UIViewController <AVCaptureMetadataOutputObjectsDelegate>
And in .m
@interface FEQRViewController ()
@property (nonatomic) BOOL isReading;
@property (nonatomic, strong) AVCaptureSession *captureSession;
@property (nonatomic, strong) AVCaptureVideoPreviewLayer *videoPreviewLayer;
-(BOOL)startReading;
-(void)stopReading;
@end
@implementation FEQRViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = ....;
self.isReading = NO;
self.captureSession = nil;
// Do any additional setup after loading the view from its nib.
}
-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
if (!self.isReading) {
if ([self startReading]) {
//[self.startButton setTitle:@"Stop" forState:UIControlStateNormal];
[self.statusLabel setText:@"Scanning for QR Code..." ];
}
}
else{
[self stopReading];
[self.startButton setTitle:@"Start!" forState:UIControlStateNormal];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)startReading
{
NSError *error;
AVCaptureDevice *captureDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:captureDevice error:&error];
if (!input) {
NSLog(@"%@", [error localizedDescription]);
return NO;
}
self.captureSession = [[AVCaptureSession alloc] init];
[self.captureSession addInput:input];
AVCaptureMetadataOutput *captureMetadataOutput = [[AVCaptureMetadataOutput alloc] init];
[self.captureSession addOutput:captureMetadataOutput];
dispatch_queue_t dispatchQueue;
dispatchQueue = dispatch_queue_create("myQueue", NULL);
[captureMetadataOutput setMetadataObjectsDelegate:self queue:dispatchQueue];
[captureMetadataOutput setMetadataObjectTypes:[NSArray arrayWithObject:AVMetadataObjectTypeQRCode]];
self.videoPreviewLayer = [[AVCaptureVideoPreviewLayer alloc] initWithSession:self.captureSession];
[self.videoPreviewLayer setVideoGravity:AVLayerVideoGravityResizeAspectFill];
[self.videoPreviewLayer setFrame:self.preview.layer.bounds];
[self.preview.layer addSublayer:_videoPreviewLayer];
[_captureSession startRunning];
return YES;
}
-(void)stopReading
{
[self.captureSession stopRunning];
self.captureSession = nil;
[self.videoPreviewLayer removeFromSuperlayer];
}
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects != nil && [metadataObjects count] > 0) {
AVMetadataMachineReadableCodeObject *metadataObj = [metadataObjects objectAtIndex:0];
if ([[metadataObj type] isEqualToString:AVMetadataObjectTypeQRCode]) {
[self.statusLabel performSelectorOnMainThread:@selector(setText:) withObject:[metadataObj stringValue] waitUntilDone:NO];
NSURL *url = [NSURL URLWithString:[metadataObj stringValue]];
if (url)
[self performSelectorOnMainThread:@selector(goToURL:) withObject:url waitUntilDone:NO];
[self performSelectorOnMainThread:@selector(stopReading) withObject:nil waitUntilDone:NO];
//[self.startButton performSelectorOnMainThread:@selector(setTitle:) withObject:@"Start!" waitUntilDone:NO];
_isReading = NO;
}
}
}
-(void)goToURL:(NSURL *)url
{
//Handle URL...
}
- (IBAction)startButton:(id)sender {
if (!self.isReading) {
if ([self startReading]) {
[self.startButton setTitle:@"Stop" forState:UIControlStateNormal];
[self.statusLabel setText:@"Scanning for QR Code..." ];
}
}
else{
[self stopReading];
[self.startButton setTitle:@"Start!" forState:UIControlStateNormal];
}
_isReading = !_isReading;
}
@end