为什么在swiftui位置弹出授权很快消失又重新出现?

问题描述

在我的应用中,我需要获得位置许可。我的应用程序分为几个组件。在 contentView 文件中,我调用为我提供位置信息的 locationProvider 类。然后,我将这些信息之一传递到显示当前位置或当前标题的组件中。

令人担忧的是,当显示权限模式时,它出现然后很快消失。我已经查看了这方面的问题,例如 here。但是这个没有帮助,因为它有点不同;

我的contentView

import UIKit
import CoreLocation

public extension CGFloat {
  var degreesToradians: CGFloat { return self * .pi / 180 }
}

struct ContentView: View {
    
  @Observedobject var location: LocationProvider = LocationProvider()
      
  private var windowWidth: CGFloat = UIScreen.main.bounds.width
  
  @State private var heading: CGFloat = .zero
  @State private var city: String = ""
  
  var body: some View {
    vstack() {
                             
      Image("arrow")
        .resizable()
        .aspectRatio(contentMode: .fit)
        .rotationEffect(.degrees(Double(-self.heading)))
        .animation(.easeInOut(duration: 0.2))
        .frame(width: 300,height: 300)
        .onReceive(self.location.heading) { heading in
          self.heading = CGFloat(heading)
        }
      
      degrees(heading: $heading)
      
      Location(city: $city)
    }
    .padding(.horizontal,20)
    .padding(.vertical,40)
    .onReceive(self.location.city) { city in
      self.city = city
    }
  }
}

LocationProvider 文件

import SwiftUI
import CoreLocation
import Combine

public class LocationProvider: NSObject,CLLocationManagerDelegate,ObservableObject {
  
  // Location manager
  private let locationManager: CLLocationManager = CLLocationManager()
  
  // heading
  public let heading = PassthroughSubject<CGFloat,Never>()
  public let city = PassthroughSubject<String,Never>()
  
  @Published var currentheading: CGFloat {
    willSet { heading.send(newValue) }
  }
  
  @Published var currentCity: String {
    willSet { city.send(newValue) }
  }
  
  public override init() {
      
    currentheading = 0
    currentCity = ""
    
    super.init()
    
    //
    // Location manager
    //
    locationManager.delegate = self
    
    self.setup()
  }
  
  private func setup() {
    // Réglage de la préscision
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestWhenInUseAuthorization()
    locationManager.startUpdatingLocation()
    locationManager.startUpdatingheading()
  }
  
  //
  //  Location manager - Update heading
  //  Calcule de l'angle et de la destination
  //
  public func locationManager(_ manager: CLLocationManager,didUpdateheading newheading: CLheading) {
    self.currentheading = CGFloat(newheading.magneticheading)
  }
  
  public func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
    guard let location = locations.last else { return }
    
    getLocation(from: location) { city,administrativeArea,error in
      guard let city = city,error == nil else { return }
      
      self.currentCity = city
    }
  }
  
  public func getLocation(from location: CLLocation,completion: @escaping (_ city: String?,_ administrativeArea: String?,_ error: Error?) -> ()) {
    
    CLGeocoder().reverseGeocodeLocation(location) { placemarks,error in
      completion(placemarks?.first?.locality,placemarks?.first?.administrativeArea,error)
    }
  }
}

Location 组件文件

import UIKit
import CoreLocation

struct Location: View {
  
  @Binding var city: String
  
  var body: some View {
    vstack() {
      Text("\(self.city)")
        .font(Font.system(size: 24,weight: .thin,design: .default))
    }
  }
}

我知道如果我在组件中声明 @Observedobject var location: LocationProvider = LocationProvider() 行,问题就不会出现。但这不是我想要的。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

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