使用 SwiftUI + Combine 更改位置授权状态时导航到下一个屏幕

Navigate to next screen when Location Authorisation Status Change using SwiftUI + Combine

下面是代码。在这里,LocationManager 是 Observable class,我在其中将“locationStatus”属性 标记为@Published。 现在,我想要实现的是当用户看到位置弹出窗口并提供位置访问权限时,它应该导航到下一个屏幕。有人可以帮我解决这个问题吗? 提前致谢

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

我无法提供逐行代码,但我可以向您展示一个您可以接受的想法。而是制作 CLLocationStatus @published,将其设为 propertyObserver,当更改设置为 boolean @published 属性 为 true 将触发 UI 刷新,并为您处理 navigation..

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