BUG
无故事王国
2023-11-17 e97b863bfafb2fc0fd870abc933276aa96a17a45
WanPai/Common/VC/CommonScanQRCodeVC.swift
@@ -63,44 +63,116 @@
         }
      }
      if(!checkCameraAuth()){
         let alertController = UIAlertController(title: nil,message: "请在iphone的“设置-隐私-相机”选项中,允许应用访问你的相机", preferredStyle: .alert)
         let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { action in
            self.navigationController?.popViewController(animated: true)
         })
         let okAction = UIAlertAction(title: "前往", style: .default, handler: {action in
            let settingUrl = NSURL(string: UIApplication.openSettingsURLString)!
            if UIApplication.shared.canOpenURL(settingUrl as URL){
               if #available(iOS 10.0, *) {
                  UIApplication.shared.open(settingUrl as URL, options: [:], completionHandler: nil)
               } else {
                  UIApplication.shared.openURL(settingUrl as URL)
               }
      let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
      if authStatus == AVAuthorizationStatus.notDetermined {
         AVCaptureDevice.requestAccess(for: AVMediaType.video) { (granted) in
            if granted{
               self.limitSuccess()
            }else{
               self.limitFaild()
            }
         })
         alertController.addAction(cancelAction)
         alertController.addAction(okAction)
         self.present(alertController, animated: true, completion: nil)
      }
      setupCaptureSession()
      addSubviews()
      scanTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(startAnimation), userInfo: nil, repeats: true)
   }
   public override func viewWillAppear(_ animated: Bool) {
      firstResult = true
      if let session = self.session {
         let queue = DispatchQueue(label: "startRun")
         queue.async {
            session.startRunning()
         }
      } else if authStatus == AVAuthorizationStatus.restricted || authStatus == AVAuthorizationStatus.denied {
         limitFaild()
      } else {
         limitSuccess()
      }
      scanTimer?.fireDate = NSDate.distantPast
//      if(!checkCameraAuth()){
//
//         let authStatus = AVCaptureDevice.authorizationStatus(for: AVMediaType.video)
//         if authStatus == AVAuthorizationStatus.notDetermined {
//            AVCaptureDevice.requestAccess(for: AVMediaType.video) {[weak self] (granted) in
//               if granted{
//                  self?.setupCaptureSession()
//                  self?.addSubviews()
//                  self?.scanTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(startAnimation), userInfo: nil, repeats: true)
//               }
//            }
//         } else if authStatus == AVAuthorizationStatus.restricted || authStatus == AVAuthorizationStatus.denied {
//            let alertController = UIAlertController(title: nil,message: "请在iphone的“设置-隐私-相机”选项中,允许应用访问你的相机", preferredStyle: .alert)
//            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { action in
//               self.navigationController?.popViewController(animated: true)
//            })
//
//            let okAction = UIAlertAction(title: "前往", style: .default, handler: {action in
//               let settingUrl = NSURL(string: UIApplication.openSettingsURLString)!
//               if UIApplication.shared.canOpenURL(settingUrl as URL){
//                  if #available(iOS 10.0, *) {
//                     UIApplication.shared.open(settingUrl as URL, options: [:], completionHandler: nil)
//                  } else {
//                     UIApplication.shared.openURL(settingUrl as URL)
//                  }
//               }
//            })
//
//            alertController.addAction(cancelAction)
//            alertController.addAction(okAction)
//            self.present(alertController, animated: true, completion: nil)
//         } else {
//
//         }
//
//      }else{
//         setupCaptureSession()
//         addSubviews()
//         scanTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(startAnimation), userInfo: nil, repeats: true)
//      }
   }
   private func limitSuccess(){
      setupCaptureSession()
      DispatchQueue.main.async {
         self.addSubviews()
         self.scanTimer = Timer.scheduledTimer(timeInterval: 3.0, target: self, selector: #selector(self.startAnimation), userInfo: nil, repeats: true)
         self.firstResult = true
         if let session = self.session {
            let queue = DispatchQueue(label: "startRun")
            queue.async {
               session.startRunning()
            }
         }
         self.scanTimer?.fireDate = NSDate.distantPast
      }
   }
   private func limitFaild(){
      let alertController = UIAlertController(title: nil,message: "请在iphone的“设置-隐私-相机”选项中,允许应用访问你的相机", preferredStyle: .alert)
      let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: { action in
         self.navigationController?.popViewController(animated: true)
      })
      let okAction = UIAlertAction(title: "前往", style: .default, handler: {action in
         let settingUrl = NSURL(string: UIApplication.openSettingsURLString)!
         if UIApplication.shared.canOpenURL(settingUrl as URL){
            if #available(iOS 10.0, *) {
               UIApplication.shared.open(settingUrl as URL, options: [:], completionHandler: nil)
            } else {
               UIApplication.shared.openURL(settingUrl as URL)
            }
         }
      })
      alertController.addAction(cancelAction)
      alertController.addAction(okAction)
      self.present(alertController, animated: true, completion: nil)
   }
//   public override func viewWillAppear(_ animated: Bool) {
//      firstResult = true
//      if let session = self.session {
//         let queue = DispatchQueue(label: "startRun")
//         queue.async {
//            session.startRunning()
//         }
//      }
//      scanTimer?.fireDate = NSDate.distantPast
//   }
   func checkCameraAuth() -> Bool {
      let status = AVCaptureDevice.authorizationStatus(for: .video)
      return  status == .authorized;