[UPDATE] Documentation and example project to check whether the reader is available
This commit is contained in:
parent
e0c3824442
commit
42a93f814a
|
@ -31,6 +31,7 @@ class ViewController: UIViewController, QRCodeReaderViewControllerDelegate {
|
||||||
lazy var reader = QRCodeReaderViewController(metadataObjectTypes: [AVMetadataObjectTypeQRCode])
|
lazy var reader = QRCodeReaderViewController(metadataObjectTypes: [AVMetadataObjectTypeQRCode])
|
||||||
|
|
||||||
@IBAction func scanAction(sender: AnyObject) {
|
@IBAction func scanAction(sender: AnyObject) {
|
||||||
|
if QRCodeReader.isAvailable() && QRCodeReader.supportsMetadataObjectTypes() {
|
||||||
reader.modalPresentationStyle = .FormSheet
|
reader.modalPresentationStyle = .FormSheet
|
||||||
reader.delegate = self
|
reader.delegate = self
|
||||||
|
|
||||||
|
@ -40,6 +41,13 @@ class ViewController: UIViewController, QRCodeReaderViewControllerDelegate {
|
||||||
|
|
||||||
presentViewController(reader, animated: true, completion: nil)
|
presentViewController(reader, animated: true, completion: nil)
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
let alert = UIAlertController(title: "Error", message: "Reader not supported by the current device", preferredStyle: .Alert)
|
||||||
|
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: nil))
|
||||||
|
|
||||||
|
presentViewController(alert, animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// MARK: - QRCodeReader Delegate Methods
|
// MARK: - QRCodeReader Delegate Methods
|
||||||
|
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
Pod::Spec.new do |s|
|
Pod::Spec.new do |s|
|
||||||
s.name = 'QRCodeReader.swift'
|
s.name = 'QRCodeReader.swift'
|
||||||
s.version = '3.1.0'
|
s.version = '3.1.1'
|
||||||
s.license = 'MIT'
|
s.license = 'MIT'
|
||||||
s.summary = 'Simple QRCode reader in Swift'
|
s.summary = 'Simple QRCode and 1D bar code reader in Swift'
|
||||||
s.homepage = 'https://github.com/yannickl/QRCodeReader.swift.git'
|
s.homepage = 'https://github.com/yannickl/QRCodeReader.swift.git'
|
||||||
s.social_media_url = 'https://twitter.com/yannickloriot'
|
s.social_media_url = 'https://twitter.com/yannickloriot'
|
||||||
s.authors = { 'Yannick Loriot' => 'contact@yannickloriot.com' }
|
s.authors = { 'Yannick Loriot' => 'contact@yannickloriot.com' }
|
||||||
|
|
|
@ -164,15 +164,18 @@ public final class QRCodeReader: NSObject, AVCaptureMetadataOutputObjectsDelegat
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Checks and return whether the given metadata object types are available
|
/// Checks and return whether the given metadata object types are supported by the current device
|
||||||
class func areMetadataObjectTypesAvailable(metadataObjectTypes: [String]) -> Bool {
|
class func supportsMetadataObjectTypes(_ metadataTypes: [String]? = nil) -> Bool {
|
||||||
if !isAvailable() {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
let output = AVCaptureMetadataOutput()
|
let output = AVCaptureMetadataOutput()
|
||||||
|
|
||||||
for metadataObjectType in metadataObjectTypes {
|
var metadataObjectTypes = metadataTypes
|
||||||
|
|
||||||
|
if metadataObjectTypes == nil || metadataObjectTypes?.count == 0 {
|
||||||
|
// Check the QRCode metadata object type by default
|
||||||
|
metadataObjectTypes = [AVMetadataObjectTypeQRCode]
|
||||||
|
}
|
||||||
|
|
||||||
|
for metadataObjectType in metadataObjectTypes! {
|
||||||
if !contains(output.availableMetadataObjectTypes, { $0 as String == metadataObjectType }) {
|
if !contains(output.availableMetadataObjectTypes, { $0 as String == metadataObjectType }) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ $ touch Podfile
|
||||||
$ edit Podfile
|
$ edit Podfile
|
||||||
source 'https://github.com/CocoaPods/Specs.git'
|
source 'https://github.com/CocoaPods/Specs.git'
|
||||||
platform :ios, '8.0'
|
platform :ios, '8.0'
|
||||||
pod 'QRCodeReader.swift', '~> 3.1.0'
|
pod 'QRCodeReader.swift', '~> 3.1.1'
|
||||||
```
|
```
|
||||||
|
|
||||||
Install into your project:
|
Install into your project:
|
||||||
|
@ -41,7 +41,7 @@ Open your project in Xcode from the .xcworkspace file (not the usual project fil
|
||||||
$ open MyProject.xcworkspace
|
$ open MyProject.xcworkspace
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that you have to use CocoaPods version 0.36, the pre-released version which supports swift. If you don't already have it, you can grab it with a single command.
|
*Note that you have to use CocoaPods version 0.36, the pre-released version which supports swift. If you don't already have it, you can grab it with a single command.*
|
||||||
|
|
||||||
``` bash
|
``` bash
|
||||||
[sudo] gem install cocoapods --pre
|
[sudo] gem install cocoapods --pre
|
||||||
|
@ -84,6 +84,8 @@ func readerDidCancel(reader: QRCodeReader) {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
*Note that you should check whether the device supports the reader library by using the `QRCodeReader.isAvailable()` and the `QRCodeReader.supportsMetadataObjectTypes()` methods.*
|
||||||
|
|
||||||
## Contact
|
## Contact
|
||||||
|
|
||||||
Yannick Loriot
|
Yannick Loriot
|
||||||
|
|
Loading…
Reference in New Issue