NCLoginQRCode.swift 4.1 KB

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