NCLoginQRCode.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // NCLoginQRCode.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/03/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import QRCodeReader
  25. @objc public protocol NCLoginQRCodeDelegate {
  26. @objc func dismissQRCode(_ value: String?, metadataType: String?)
  27. }
  28. class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate {
  29. lazy var reader: QRCodeReader = QRCodeReader()
  30. weak var delegate: UIViewController?
  31. lazy var readerVC: QRCodeReaderViewController = {
  32. let builder = QRCodeReaderViewControllerBuilder {
  33. $0.reader = QRCodeReader(metadataObjectTypes: [.qr], captureDevicePosition: .back)
  34. $0.showTorchButton = true
  35. $0.preferredStatusBarStyle = .lightContent
  36. $0.showOverlayView = true
  37. $0.rectOfInterest = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)
  38. $0.reader.stopScanningWhenCodeIsFound = false
  39. }
  40. return QRCodeReaderViewController(builder: builder)
  41. }()
  42. override init() {
  43. }
  44. @objc public init(delegate: UIViewController) {
  45. self.delegate = delegate
  46. }
  47. @objc func scan() {
  48. guard checkScanPermissions() else { return }
  49. readerVC.modalPresentationStyle = .formSheet
  50. readerVC.delegate = self
  51. readerVC.completionBlock = { (result: QRCodeReaderResult?) in
  52. if let result = result {
  53. print("Completion with result: \(result.value) of type \(result.metadataType)")
  54. } else {
  55. }
  56. self.readerVC.dismiss(animated: true, completion: nil)
  57. }
  58. delegate?.present(readerVC, animated: true, completion: nil)
  59. }
  60. private func checkScanPermissions() -> Bool {
  61. do {
  62. return try QRCodeReader.supportsMetadataObjectTypes()
  63. } catch let error as NSError {
  64. let alert: UIAlertController
  65. switch error.code {
  66. case -11852:
  67. alert = UIAlertController(title: "Error", message: "This app is not authorized to use Back Camera.", preferredStyle: .alert)
  68. alert.addAction(UIAlertAction(title: "Setting", style: .default, handler: { (_) in
  69. DispatchQueue.main.async {
  70. if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
  71. UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)
  72. }
  73. }
  74. }))
  75. alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
  76. default:
  77. alert = UIAlertController(title: "Error", message: "Reader not supported by the current device", preferredStyle: .alert)
  78. alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
  79. }
  80. delegate?.present(alert, animated: true, completion: nil)
  81. return false
  82. }
  83. }
  84. func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
  85. reader.stopScanning()
  86. (self.delegate as? NCLoginQRCodeDelegate)?.dismissQRCode(result.value, metadataType: result.metadataType)
  87. }
  88. func readerDidCancel(_ reader: QRCodeReaderViewController) {
  89. reader.stopScanning()
  90. (self.delegate as? NCLoginQRCodeDelegate)?.dismissQRCode(nil, metadataType: nil)
  91. }
  92. }