2017-04-10 02:15:16 +08:00
|
|
|
//
|
|
|
|
|
// QRScannerController.swift
|
|
|
|
|
// pass
|
|
|
|
|
//
|
|
|
|
|
// Created by Yishi Lin on 10/4/17.
|
|
|
|
|
// Copyright © 2017 Yishi Lin. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
|
import AVFoundation
|
2017-06-13 11:42:49 +08:00
|
|
|
import passKit
|
2017-04-10 02:15:16 +08:00
|
|
|
|
|
|
|
|
class OTPScannerController: QRScannerController {
|
|
|
|
|
|
|
|
|
|
var scannedOTP: String?
|
|
|
|
|
|
|
|
|
|
// MARK: - AVCaptureMetadataOutputObjectsDelegate Methods
|
|
|
|
|
override func captureOutput(_ captureOutput: AVCaptureOutput!, didOutputMetadataObjects metadataObjects: [Any]!, from connection: AVCaptureConnection!) {
|
|
|
|
|
|
|
|
|
|
if let metadataObj = metadataObjects.first as? AVMetadataMachineReadableCodeObject,
|
|
|
|
|
supportedCodeTypes.contains(metadataObj.type),
|
|
|
|
|
let barCodeObject = videoPreviewLayer?.transformedMetadataObject(for: metadataObj) {
|
|
|
|
|
|
|
|
|
|
// draw a bounds on the found QR code
|
|
|
|
|
qrCodeFrameView?.frame = barCodeObject.bounds
|
|
|
|
|
|
|
|
|
|
// check whether it is a valid result
|
|
|
|
|
if let scannedString = metadataObj.stringValue {
|
|
|
|
|
if let (accept, message) = delegate?.checkScannedOutput(line: scannedString) {
|
2017-04-11 23:00:23 +08:00
|
|
|
scannerOutput.text = message
|
2017-04-10 02:15:16 +08:00
|
|
|
if accept == true {
|
|
|
|
|
captureSession?.stopRunning()
|
|
|
|
|
scannedOTP = scannedString
|
2017-04-11 23:00:23 +08:00
|
|
|
presentSaveAlert()
|
2017-04-10 02:15:16 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// no delegate, show the scanned result
|
|
|
|
|
scannerOutput.text = scannedString
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
scannerOutput.text = "No string value"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
qrCodeFrameView?.frame = CGRect.zero
|
|
|
|
|
scannerOutput.text = "No QR code detected"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-04-11 23:00:23 +08:00
|
|
|
private func presentSaveAlert() {
|
|
|
|
|
// initialize alert
|
2017-04-23 10:06:31 -07:00
|
|
|
let password = Password(name: "empty", url: nil, plainText: scannedOTP!)
|
2017-04-11 23:00:23 +08:00
|
|
|
let (title, content) = password.getOtpStrings()!
|
|
|
|
|
let alert = UIAlertController(title: "Success", message: "\(title): \(content)", preferredStyle: UIAlertControllerStyle.alert)
|
|
|
|
|
alert.addAction(UIAlertAction(title: "Save", style: UIAlertActionStyle.default, handler: {[unowned self] (action) -> Void in
|
|
|
|
|
self.delegate?.handleScannedOutput(line: self.scannedOTP!)
|
|
|
|
|
self.navigationController?.popViewController(animated: true)
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
if password.otpType == .hotp {
|
|
|
|
|
// hotp, no need to refresh
|
|
|
|
|
self.present(alert, animated: true, completion: nil)
|
|
|
|
|
} else if password.otpType == .totp {
|
|
|
|
|
// totp, refresh otp
|
|
|
|
|
self.present(alert, animated: true) {
|
|
|
|
|
let alertController = self.presentedViewController as! UIAlertController
|
|
|
|
|
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) {_ in
|
2017-04-10 02:15:16 +08:00
|
|
|
let (title, content) = password.getOtpStrings()!
|
2017-04-11 23:00:23 +08:00
|
|
|
alertController.message = "\(title): \(content)"
|
2017-04-10 02:15:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|