QRCodeReader.swift/Example/QRCodeReader.swift/ViewController.swift

136 lines
4.8 KiB
Swift

/*
* QRCodeReader.swift
*
* Copyright 2014-present Yannick Loriot.
* http://yannickloriot.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
import AVFoundation
import UIKit
class ViewController: UIViewController, QRCodeReaderViewControllerDelegate {
@IBOutlet weak var previewView: QRCodeReaderView!
lazy var reader: QRCodeReader = QRCodeReader()
lazy var readerVC: QRCodeReaderViewController = {
let builder = QRCodeReaderViewControllerBuilder {
$0.reader = QRCodeReader(metadataObjectTypes: [AVMetadataObject.ObjectType.qr], captureDevicePosition: .back)
$0.showTorchButton = true
}
return QRCodeReaderViewController(builder: builder)
}()
// MARK: - Actions
private func checkScanPermissions() -> Bool {
do {
return try QRCodeReader.supportsMetadataObjectTypes()
} catch let error as NSError {
let alert: UIAlertController?
switch error.code {
case -11852:
alert = UIAlertController(title: "Error", message: "This app is not authorized to use Back Camera.", preferredStyle: .alert)
alert?.addAction(UIAlertAction(title: "Setting", style: .default, handler: { (_) in
DispatchQueue.main.async {
if let settingsURL = URL(string: UIApplicationOpenSettingsURLString) {
UIApplication.shared.openURL(settingsURL)
}
}
}))
alert?.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
case -11814:
alert = UIAlertController(title: "Error", message: "Reader not supported by the current device", preferredStyle: .alert)
alert?.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
default:
alert = nil
}
guard let vc = alert else { return false }
present(vc, animated: true, completion: nil)
return false
}
}
@IBAction func scanInModalAction(_ sender: AnyObject) {
guard checkScanPermissions() else { return }
readerVC.modalPresentationStyle = .formSheet
readerVC.delegate = self
readerVC.completionBlock = { (result: QRCodeReaderResult?) in
if let result = result {
print("Completion with result: \(result.value) of type \(result.metadataType)")
}
}
present(readerVC, animated: true, completion: nil)
}
@IBAction func scanInPreviewAction(_ sender: Any) {
guard checkScanPermissions(), !reader.isRunning else { return }
previewView.setupComponents(showCancelButton: false, showSwitchCameraButton: false, showTorchButton: false, showOverlayView: true, reader: reader)
reader.startScanning()
reader.didFindCode = { result in
let alert = UIAlertController(
title: "QRCodeReader",
message: String (format:"%@ (of type %@)", result.value, result.metadataType),
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
}
}
// MARK: - QRCodeReader Delegate Methods
func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
reader.stopScanning()
dismiss(animated: true) { [weak self] in
let alert = UIAlertController(
title: "QRCodeReader",
message: String (format:"%@ (of type %@)", result.value, result.metadataType),
preferredStyle: .alert
)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self?.present(alert, animated: true, completion: nil)
}
}
func reader(_ reader: QRCodeReaderViewController, didSwitchCamera newCaptureDevice: AVCaptureDeviceInput) {
print("Switching capturing to: \(newCaptureDevice.device.localizedName)")
}
func readerDidCancel(_ reader: QRCodeReaderViewController) {
reader.stopScanning()
dismiss(animated: true, completion: nil)
}
}