Modify from https://github.com/yannickl/QRCodeReaderViewController
If you're using CocoaPods (You are not?! You should!!) just add
pod 'QRCodeReaderViewController', :git => 'https://github.com/zhengjinghua-ext-forks/QRCodeReaderViewController.git'
into your Podfile file.
Download the project and copy the QRCodeReaderViewController
folder into your project and then simply #import "QRCodeReaderViewController.h"
in the file(s) you would like to use it in.
- (IBAction)scanAction:(id)sender
{
NSArray *types = @[AVMetadataObjectTypeQRCode];
_reader = [QRCodeReaderViewController readerWithMetadataObjectTypes:types];
// Using delegate methods
_reader.delegate = self;
// Or by using blocks
[_reader setCompletionWithBlock:^(NSString *resultAsString) {
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"%@", resultAsString);
}];
}];
[self presentViewController:_reader animated:YES completion:NULL];
}
#pragma mark - QRCodeReader Delegate Methods
- (void)reader:(QRCodeReaderViewController *)reader didScanResult:(NSString *)result
{
[self dismissViewControllerAnimated:YES completion:^{
NSLog(@"%@", result);
}];
}
- (void)readerDidCancel:(QRCodeReaderViewController *)reader
{
[self dismissViewControllerAnimated:YES completion:NULL];
}
Note that you should check whether the device supports the reader library by using the [QRCodeReader isAvailable]
or the [QRCodeReader supportsMetadataObjectTypes:nil]
methods.