QRKit 1.0.1

QRKit 1.0.1

Drew Wilken 维护。



QRKit 1.0.1

  • 作者:
  • Drew Wilken

QRKit

一个为初学者编写的简单、轻量级的框架,用于处理二维码 - 读取和生成。 目前还在进行中,已经可以工作,但仍然是进行中

包含内容

该框架包括双重功能 - 生成和读取二维码。

安装/集成

我将很快包括 carthage 和 cocoa pod 兼容性,但截至目前,这不是它的稳定版本。

要手动安装:

  • 下载/克隆项目
  • 将 xcodeproj 文件拖动到 Xcode 中的项目内,并嵌套
  • 在文件结构中点击您要使用其的 xcodeproj,然后转到“常规”选项卡
  • 在“已嵌入的二进制文件”下,点击加号
  • 添加 QRKit 框架
  • 进入您想使用的视图控制器,并输入 import QRKit

使用方法

使用 QRKit 很简单,与不使用 QRKit 执行这些操作的完整代码相比,所需代码很少。目前,QRKit 支持二维码生成和读取。

以下是一个使用该框架的示例应用程序:https://github.com/Drewdubeast/QRKitSample

如何生成二维码

  • 前往主.storyboard
  • 在你的视图中添加一个新的图像视图
  • 进入属性管理器,将图像视图的类改为 QRView
  • 将视图链接到你的ViewController
  • 使用任何想要编码的字符串调用你的图像视图上的setupQR()
    • 例如:myImageView.setupQR(string: "Hello, world!")
    • 你的ViewController将如下所示
    import UIKit
    import QRKit
    
    class QRGenViewController: UIViewController {
    
      @IBOutlet weak var QRSampleView: QRView!
      
      override func viewDidLoad() {
          super.viewDidLoad()
          
          //generate the QR code
          QRSampleView.setupQR(string: "Hello, World!")
      }
    }
  • 运行!

如何读取和处理二维码

这可以通过显示框架中包含的QR读取器来完成。这应该在ViewDidAppear()函数中这样做

override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        //create camera view controller
        let camera = QRReaderController.init()
        
        //show the camera view controller
        present(camera, animated: true, completion: nil)
}

处理二维码

每当扫描到一个二维码且有数据时,都会调用代表的processQR()。因此,为了能够在扫描二维码时进行处理,你的ViewController必须设置为代表

为了将视图控制器设置为代表,您的ViewController必须遵守QRReaderDelegate协议

public protocol QRReaderDelegate {
    func processQR(_ qrString: String, with controller: UIViewController)
}

典型的简单方法是将扩展添加到您的ViewController

//delegate to handle QR findings - must implement in order to be able to process QR code findings.
extension ViewController: QRReaderDelegate {
    func processQR(_ qrString: String, with controller: UIViewController) {
        print(qrString)
    } 
}

上面的实现只会打印QR数据,但更实际的途径是使用alert控制器来处理,例如

//delegate to handle QR findings - must implement in order to be able to process QR code findings.
extension ViewController: QRReaderDelegate {
    func processQR(_ qrString: String, with controller: UIViewController) {
        let alert = UIAlertController(title: "FOUND A CODE BOI", message: qrString, preferredStyle: .alert)
        
        alert.addAction(UIAlertAction(title: "Confirm", style: .default, handler: nil))
        
        controller.present(alert, animated: true)
    } 
}

这意味着每当扫描并读取到一个二维码时,都会在您的ViewController中调用这个方法,所以您在这个函数中编写的代码将在每次读取新二维码时执行。

一旦遵守了协议,可以将框架视图的代表设置为self,如下所示

//create camera view controller
let camera = QRReaderController.init()
        
//set delegate to self (so that when a QR code is read, it sends the data to this class
camera.delegate = self

贡献

我始终欢迎贡献并推动这个想法的发展。虽然它还比较新,但我正在努力将其发展成为一种许多人都可以使用,真正有帮助的东西。目前来讲,这是一个很酷的想法,但并不一定是 最有帮助 的东西。

要做出贡献,请发起一个请求问题的 issue,我就会邀请你成为贡献者。非常感谢!