QR Code Scanner for iOS
使用设备的相机扫描 QR 码并返回一个字符串值。
安装
-
安装 CocoaPods。
-
在您的 Podspec 文件中添加以下依赖项
pod 'QR-Code-Scanner-iOS', '~> 1.0'
-
运行
pod install
。
使用方法
import UIKit
import QRCodeScanner
class MyViewController: UIViewController, QRCodeScanViewControllerDelegate {
/// Start the QR code scan
func scanQRCode() {
// Create an instance of QRCodeScanViewController
let viewController = QRCodeScanViewController.create()
// Set itself as delegate
viewController.delegate = self
// Present the view controller
self.present(viewController, animated: true)
}
// MARK: QRCodeScanViewControllerDelegate
/// Called when the camera scans a QR code
/// - Parameters:
/// - viewController: View controller that scanned the QR code
/// - value: String encoded in the QR code
func qrCodeScanViewController(_ viewController: QRCodeScanViewController, didScanQRCode value: String) {
// Dismiss the view controller
viewController.dismiss(animated: true) {
// Show an alert with the scanned value
let alert = UIAlertController(title: "Scanned value", message: value, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true)
}
}
}