问题描述
当我在 XCode13 的 B3 模拟器上运行 LocationButton 时,我得到 Error Domain=kCLErrorDomain Code=1 "(null) 当按钮被按下时 使用委托 func locationManager(_ manager: CLLocationManager,didFailWithError error: Error) 检测错误 列表如下,我认为原因是Plist。 不知道Plist和iOS14的关系。 未使用 Plist。
`
import SwiftUI
import CoreLocation
import CoreLocationUI
import MapKit
class LocationButtonviewmodel: NSObject,ObservableObject,CLLocationManagerDelegate {
private lazy var manager = CLLocationManager()
private static let span = MKCoordinateSpan(latitudeDelta: 0.008,longitudeDelta: 0.008)
@Published var region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 37.408672852282024,longitude: -5.944668759610994),span: LocationButtonviewmodel.span)
@Published var fetchingLocation: Bool = false
override init() {
super.init()
manager.delegate = self
}
func requestLocation() {
fetchingLocation = true
manager.requestLocation()
}
func locationManager(_ manager: CLLocationManager,didUpdateLocations locations: [CLLocation]) {
if let location = locations.first?.coordinate {
region = MKCoordinateRegion(center: location,span: LocationButtonviewmodel.span)
}
fetchingLocation = false
}
func locationManager(_ manager: CLLocationManager,didFailWithError error: Error) {
print(error)
fetchingLocation = false
}
}
struct ContentView: View {
@Observedobject var model = LocationButtonviewmodel()
var body: some View {
GeometryReader { proxy in
Map(coordinateRegion: $model.region)
.overlay(
Group {
if model.fetchingLocation {
ProgressView()
.tint(.black)
} else {
LocationButton(.currentLocation) {
model.requestLocation()
}
.overlay(
Circle()
.stroke(.gray,linewidth: 1)
)
}
}
.frame(width: 44,height: 44)
.cornerRadius(22)
.labelStyle(.iconOnly)
.symbolVariant(.fill)
.tint(.white)
.offset(x: proxy.size.width / 2 - 44,y: proxy.size.height / 2 - 64)
.padding()
)
}
}
}
`
解决方法
您是否在 info.plist 文件中添加了适当的键值对?
隐私 - 使用时的位置使用说明:在此处向用户显示的消息