您如何让位置捕获器在优先选择之前运行?

问题描述

因此,我需要先捕获用户的位置(当然要得到他们的许可),然后再访问首页。因此,在登录/注册页面上,我想捕获位置,但是在覆盖登录用户首页之前,我需要这样做。你们有什么想法吗?以下是优先顺序。

我在覆盖功能中将位置捕获器放置在segue之前,但是在用户有机会接受共享位置之前,它仍转到主页。这并不是真正的意外,因为根据定义,整个覆盖功能都是覆盖功能

  override func viewDidAppear(_ animated: Bool) {
//////////////////////
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation])   {

    let databaseRef = Database.database().reference()
    guard let uid = Auth.auth().currentUser?.uid else { return }
    guard let locValue: CLLocationCoordinate2D = manager.location?.coordinate else { return }
    print("locations = \(locValue.latitude) \(locValue.longitude)")
    latestLocation = ["latitude" : locValue.latitude,"longitude" : locValue.longitude]
    let lat = locValue.latitude
    let lon = locValue.longitude
    dict = CLLocation(latitude: lat,longitude: lon)
    print("dict",dict)
    if let locationDictionary = latestLocation {           
databaseRef.child("people").child(uid).child("Coordinates").setValue(locationDictionary)  
    }   
}
//////////////////////
 if  Auth.auth().currentUser != nil {
        self.performSegue(withIdentifier: "tohome",sender: nil)
    }
}

更新:这是我登录注册的方式。

    @IBAction func Registerpressed(_ sender: Any) {
        
        Auth.auth().createuser(withEmail: (emailField.text ?? ""),password: (passwordField.text ?? ""))  { (user,error) in
            if let _eror = error {
                //something bad happning
                print(_eror.localizedDescription )
                let alert = UIAlertController(title: "Error",message: "Invalid Entry or Duplicate.",preferredStyle: UIAlertController.Style.alert)
                let action = UIAlertAction(title: "Ok",style: .default,handler: nil)
                alert.addAction(action)
                self.present(alert,animated: true,completion: nil)
                
            }else{
                //user registered successfully
                print(user as Any)
                if let userID = user?.user.uid
                {
                    KeychainWrapper.standard.set((userID),forKey: "uid")
                    
                    let databaseRef = Database.database().reference()
                    
                    databaseRef.child("A1").child(userID).child("users").setValue(self.emailField.text!)
                    databaseRef.child("people").child(userID).child("postID").setValue(userID)
                    let components = self.emailField.text!.split(separator: "@")
                    let child1 = components[0] //will be jake
                    let child2 = components[1] //will be aol.com
                    print(components,child1,child2,"childs")
                    databaseRef.child("people").child(userID).child("e2").setValue(child2)


                    components.forEach { print($0) }
                    
                    
                    

                    
                    self.performSegue(withIdentifier: "tohome",sender: nil)
                }
                
            }
        }
    }
     @IBAction func loginInpressed(_ sender: Any) {
 Auth.auth().signIn(withEmail: (emailField.text ?? ""),error) in
    if let _eror = error {
                   //something bad happning
                   print(_eror.localizedDescription )
                let alert = UIAlertController(title: "Error",message: "Incorrect Email or Password.",preferredStyle: UIAlertController.Style.alert)
                                     let action = UIAlertAction(title: "Ok",handler: nil)
                                     alert.addAction(action)
                                     self.present(alert,completion: nil)

               }else{
                   //user registered successfully
                   print(user as Any)
                        if let userID = user?.user.uid
 {
                            KeychainWrapper.standard.set((userID),forKey: "uid")
                            self.performSegue(withIdentifier: "tohome",sender: nil) }

               }
            }
    }

解决方法

如果我在viewDidAppear(_:)中正确理解它,则您有一个条件。如果是true,则您立即尝试执行segue。

if  Auth.auth().currentUser != nil {
    self.performSegue(withIdentifier: "tohome",sender: nil)
}

由于必须将位置移到HomeScreen中,为什么在segue内部收到第一个位置更新时为何不进行导航(CLLocationManagerDelegate)?

func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation])   {
      // check for existing location if we received earlier
      guard self.latestLoc == nil else {
         // we received one location earlier,navigation to Home completed. no need to proceed 
         // If we no longer need to listen for loc updates,we can stop listening further
         manager.stopUpdatingLocation()
         return
      } 
      // ... Your exiting data base update code
     guard let latestLoc = locations.last else { return }
     self.latestLoc = latestLoc // If you want to pass this location to destination view controller in `prepare(for: UIStoryboardSegue,sender: Any?)`
     // Also it will good practice to call manager.stopUpdatingLocation() if you need location only for once 

     // Now move to HomeScreen
     self.performSegue(withIdentifier: "tohome",sender: nil)

     // If we no longer need to listen for loc updates,we can stop listening further
     manager.stopUpdatingLocation()
}

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...