简体   繁体   English

您如何让位置捕获器在覆盖转场之前运行?

[英]How do you get a location capturer to run before an override segue?

So, I need to capture user's location (with their permission of course) before he gets to homepage.所以,我需要在用户访问主页之前捕获用户的位置(当然需要他们的许可)。 So on the login/register page I want to capture the location, but I need to do it before the override segue to homepage for logged in users.因此,在登录/注册页面上,我想捕获位置,但我需要在覆盖登录用户的主页之前执行此操作。 Any of you have any ideas?你们有什么想法吗? Below is the override segue.下面是覆盖segue。

I put location capturer before the segue in the override function, but it still went to homepage before the user had an opportunity to accept share location.我在覆盖功能中的 segue 之前放置了位置捕获器,但它仍然在用户有机会接受共享位置之前进入主页。 This is not really unexpected since the entire override function is by definition an override function这并不出乎意料,因为整个覆盖函数根据定义是一个覆盖函数

  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)
    }
}

Update: This is how I login and register.更新:这就是我登录和注册的方式。

    @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 ?? ""), password: (passwordField.text ?? ""))  { (user, 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", 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")
                            self.performSegue(withIdentifier: "tohome", sender: nil) }

               }
            }
    }

If I understand it correctly in viewDidAppear(_:) you have one condition.如果我在viewDidAppear(_:)正确理解它,你有一个条件。 If it's true you immediately trying to perform the segue.如果是true您会立即尝试执行转场。

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

As location is must to move into HomeScreen , why don't you do the navigation( segue ) when you receive the first location update inside CLLocationManagerDelegate ?由于位置必须移动到HomeScreen ,当您在CLLocationManagerDelegate收到第一个位置更新时,为什么不进行导航( segue )?

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()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM