仿微信二维码扫描:QRCodeReaderViewController
仿微信二维码扫描。
The QRCodeReaderViewController was initialy a simple QRCode reader but it now lets you the possibility to specify the format type you want to decode. It is based on theAVFoundationframework from Apple in order to replace ZXing or ZBar for iOS 7 and over.
It provides a default view controller to display the camera view with the scan area overlay and it also provides a button to switch between the front and the back cameras.
Installation
The recommended approach to use QRCodeReaderViewController in your project is using the CocoaPods package manager, as it provides flexible dependency management and dead simple installation.
CocoaPods
Install CocoaPods if not already available:
$ [sudo] gem install cocoapods $ pod setup
Go to the directory of your Xcode project, and Create and Edit your Podfile and add QRCodeReaderViewController:
$ cd /path/to/MyProject $ touch Podfile $ edit Podfile source 'https://github.com/CocoaPods/Specs.git' platform :ios, '7.0' pod 'QRCodeReaderViewController', '~> 3.5.2'
Install into your project:
$ pod install
Open your project in Xcode from the .xcworkspace file (not the usual project file)
$ open MyProject.xcworkspace
Manually
Download the project and copy theQRCodeReaderViewControllerfolder into your project and then simply#import "QRCodeReaderViewController.h"in the file(s) you would like to use it in.
Usage
- (IBAction)scanAction:(id)sender { NSArray *types = @[AVMetadataObjectTypeQRCode]; _reader = [QRCodeReaderViewController readerWithMetadataObjectTypes:types]; // Set the presentation style _reader.modalPresentationStyle = UIModalPresentationFormSheet; // 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]; }