0

Below is the code. Here, LocationManager is the Observable class where I have marked "locationStatus" property as @Published. Now, what I want to achieve is when user sees the location Popup and gives Location access, it should navigate to next screen. Can someone please help me with this one? Thanks in advance

 struct MyLocationScreen: View {
    
    @ObservedObject var locationManager = LocationManager()
    @State var isShowLinkTarget :Bool = false

    var body: some View {
        NavigationView{
          ZStack{
             Button(action: {

               let locStatus = self.locationManager.locationStatus

               if locStatus == .authorizedWhenInUse || locStatus == .authorizedAlways {
                   self.showLinkTarget = true
               } else {
                   self.locationManager.requestAuthorization()
                   self.locationManager.startUpdate()
               }

         }) {
             Text("Share Location")
               
            }
            NavigationLink(destination: NextScreen(), isActive: $showLinkTarget) {
                Text("")
            }
          }
        }
    }
}
Himanshu Ahuja
  • 197
  • 1
  • 13
  • Thanks for the reply @TusharSharma. In LocationManager class, locationStatus has been marked as "@ Published" var locationStatus: CLAuthorizationStatus?. So, inside of location manager instance, it already has been binded. But, In NavigationLink, we have to change isActive to true, which asks for Binded type, not a bool type. Can u please help me out how do we make it true ? – Himanshu Ahuja Mar 09 '21 at 16:01
  • @TusharSharma, If u can help me out with a code snippet, that would be a great help – Himanshu Ahuja Mar 09 '21 at 16:05

1 Answers1

1

I can’t provide a line to line code, but I can show you an idea that you can go with. Instead making CLLocationStatus @published, make it a propertyObserver, that when changes sets a boolean @published property to true which will trigger UI refresh, and also handle navigation for you..

import SwiftUI

struct MyLocationScreen: View {
    
    @ObservedObject var locationManager = LocationManager()
    @State var isShowLinkTarget :Bool = false
    
    var body: some View {
        NavigationView{
            ZStack{
                Button(action: {
                    
                    /*
                     Once locationManager(), CLLocation property changes it's going to set @published isGranted to true in model class. Which will then trigger body refresh, and navigation can use this updated state.
                     
                     NOTE-: Below i am assigning object just as an example, this has to be your  code.
                     */
                    
                    locationManager.locationStatus = CLAuthorizationStatus()
                    
                    // You can implement below two line alone
                    
                    //                self.locationManager.requestAuthorization()
                    //                self.locationManager.startUpdate()
                    
                }) {
                    Text("Share Location")
                    
                }
                NavigationLink(destination: Text(""), isActive: $locationManager.isGranted) {
                    Text("")
                }
            }
        }
    }
}


class LocationManager: ObservableObject,Identifiable {
    
    var id  = UUID().uuidString
    @Published var isGranted:Bool = false
    
    var locationStatus:CLAuthorizationStatus?{
        didSet{
            
            // Some logic if you want like
            
//            if locationStatus == .authorizedWhenInUse || locationStatus == .authorizedAlways {
//                self.isGranted = true
//            }
//
            
            isGranted = true
        }
    }
}

class CLAuthorizationStatus{
    
}
Tushar Sharma
  • 2,839
  • 1
  • 16
  • 38