【问题标题】:SwiftUI 3.0 XCode13 B3 Executing the LocationButton will result in Error Domain=kCLErrorDomain Code=1 "(null)SwiftUI 3.0 XCode13 B3 执行LocationButton会导致Error Domain=kCLErrorDomain Code=1 "(null)
【发布时间】:2021-09-28 02:15:12
【问题描述】:

当我在 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()
                    )
            }
        }
}

`

【问题讨论】:

    标签: swiftui cllocationmanager swift5.5 location-button


    【解决方案1】:

    您是否在 info.plist 文件中添加了适当的键:值对?

    隐私 - 使用时的位置 使用说明:在此处显示给用户的消息

    【讨论】:

    • 我忘记了 plist。非常感谢
    猜你喜欢
    • 2015-05-19
    • 2021-04-15
    • 2011-08-27
    • 2015-10-29
    • 2016-10-14
    • 2018-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多