NCLoginQRCode.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 UIKit
  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. self.readerVC.dismiss(animated: true, completion: nil)
  53. }
  54. delegate?.present(readerVC, animated: true, completion: nil)
  55. }
  56. private func checkScanPermissions() -> Bool {
  57. do {
  58. return try QRCodeReader.supportsMetadataObjectTypes()
  59. } catch let error as NSError {
  60. let alert: UIAlertController
  61. switch error.code {
  62. case -11852:
  63. alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_qrcode_not_authorized_", comment: ""), preferredStyle: .alert)
  64. alert.addAction(UIAlertAction(title: NSLocalizedString("_settings_", comment: ""), style: .default, handler: { (_) in
  65. DispatchQueue.main.async {
  66. if let settingsURL = URL(string: UIApplication.openSettingsURLString) {
  67. UIApplication.shared.open(settingsURL, options: [:], completionHandler: nil)
  68. }
  69. }
  70. }))
  71. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil))
  72. default:
  73. alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_qrcode_not_supported_", comment: ""), preferredStyle: .alert)
  74. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .cancel, handler: nil))
  75. }
  76. delegate?.present(alert, animated: true, completion: nil)
  77. return false
  78. }
  79. }
  80. func reader(_ reader: QRCodeReaderViewController, didScanResult result: QRCodeReaderResult) {
  81. reader.stopScanning()
  82. (self.delegate as? NCLoginQRCodeDelegate)?.dismissQRCode(result.value, metadataType: result.metadataType)
  83. }
  84. func readerDidCancel(_ reader: QRCodeReaderViewController) {
  85. reader.stopScanning()
  86. (self.delegate as? NCLoginQRCodeDelegate)?.dismissQRCode(nil, metadataType: nil)
  87. }
  88. }