NCLoginQRCode.swift 4.6 KB

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