NCLoginQRCode.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. class NCLoginQRCode: NSObject, QRCodeReaderViewControllerDelegate {
  26. lazy var reader: QRCodeReader = QRCodeReader()
  27. weak var delegate: UIViewController?
  28. lazy var readerVC: QRCodeReaderViewController = {
  29. let builder = QRCodeReaderViewControllerBuilder {
  30. $0.reader = QRCodeReader(metadataObjectTypes: [.qr], captureDevicePosition: .back)
  31. $0.showTorchButton = true
  32. $0.preferredStatusBarStyle = .lightContent
  33. $0.showOverlayView = true
  34. $0.rectOfInterest = CGRect(x: 0.2, y: 0.2, width: 0.6, height: 0.6)
  35. $0.reader.stopScanningWhenCodeIsFound = false
  36. }
  37. return QRCodeReaderViewController(builder: builder)
  38. }()
  39. override init() {
  40. }
  41. @objc public init(delegate: UIViewController) {
  42. self.delegate = delegate
  43. }
  44. @objc func scan() {
  45. guard checkScanPermissions() else { return }
  46. readerVC.modalPresentationStyle = .formSheet
  47. readerVC.delegate = self
  48. readerVC.completionBlock = { (result: QRCodeReaderResult?) in
  49. if let result = result {
  50. print("Completion with result: \(result.value) of type \(result.metadataType)")
  51. }
  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: "Error", message: "This app is not authorized to use Back Camera.", preferredStyle: .alert)
  63. alert.addAction(UIAlertAction(title: "Setting", 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: "Cancel", style: .cancel, handler: nil))
  71. default:
  72. alert = UIAlertController(title: "Error", message: "Reader not supported by the current device", preferredStyle: .alert)
  73. alert.addAction(UIAlertAction(title: "OK", 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. }
  82. func readerDidCancel(_ reader: QRCodeReaderViewController) {
  83. reader.stopScanning()
  84. }
  85. }