当我的视图出现时:
struct TurnOnLocation: View {
var body: some View {
ZStack {
Color.orange
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Image(systemName: "globe")
.resizable()
.aspectRatio(contentMode: .fill)
.foregroundColor(.white)
Spacer()
Button(action: {
let locationManager = LocationManager()
locationManager.locationManager.requestWhenInUseAuthorization()
locationManager.locationManager.startUpdatingLocation()
}) {
Text("Turn on location")
}.foregroundColor(Color.black)
.font(Font.system(size:22))
.padding(20)
.background(
Rectangle()
.fill(Color.white)
.border(Color.white, width:1)
)
.cornerRadius(50)
.shadow(radius: 10)
.padding(50)
}
}
}
}
它在点击Button
之前询问用户的位置。为什么会这样?我在下面添加了我的模型,当它初始化时(应该是点击按钮时(就会被激发。
class LocationManager: NSObject, ObservableObject {
@Published var locationStatus: CLAuthorizationStatus? = CLAuthorizationStatus.notDetermined
@Published var locationPermissionStatus = PermissionStatus.unknown
@Published var location: CLLocation?
let locationManager = CLLocationManager()
override init(){
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
enum PermissionStatus {
case unknown
case authorizedWhenInUse
case authorizedAlways
case restricted
case denied
}
func getLocation() -> CLLocationCoordinate2D? {
return self.location?.coordinate
}
}
知道我如何确保对话框只在点击按钮后显示吗?
编辑:
我已经更新了代码:
Button(action: {
let locationManager = LocationManager()
locationManager.requestPermission()
})
型号
class LocationManager: NSObject, ObservableObject , CLLocationManagerDelegate {
@Published var locationStatus: CLAuthorizationStatus? = CLAuthorizationStatus.notDetermined
@Published var locationPermissionStatus = PermissionStatus.unknown
@Published var location: CLLocation?
let locationManager = CLLocationManager()
override init(){
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func requestPermission(){
self.locationManager.requestWhenInUseAuthorization()
}
func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) { switch status {
case .restricted, .denied:
// Disable your app's location features
print("restricted")
break
case .authorizedWhenInUse:
// Enable your app's location features.
print("authorizedWhenInUse")
self.locationManager.startUpdatingLocation()
break
case .authorizedAlways:
// Enable or prepare your app's location features that can run any time.
print("authorizedAlways")
self.locationManager.startUpdatingLocation()
break
case .notDetermined:
print("notDetermined")
break
}
}
但是,当我单击该按钮时,请求定位权限的对话框在一秒钟后消失。知道为什么吗?
有两件事需要修复:
-
您已经在
init
中调用requestWhenInUseAuthorization
,这就是为什么在构建按钮时会看到警报的原因。您应该删除init
中的requestWhenInUseAuthorization
。 -
您应该在确认拥有
LocationManager
中的授权后致电startUpdatingLocation
。
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) { switch status { case .authorizedAlways, .authorizedWhenInUse: manager.startUpdatingLocation() default: print("no auth") } }
您正在LocationManager
init方法中请求授权。您应该从init中删除该代码。
class LocationManager: NSObject, ObservableObject {
@Published var locationStatus: CLAuthorizationStatus? = CLAuthorizationStatus.notDetermined
@Published var locationPermissionStatus = PermissionStatus.unknown
@Published var location: CLLocation?
let locationManager = CLLocationManager()
override init(){
super.init()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
}
func requestAuthorization() {
self.locationManager.requestWhenInUseAuthorization()
}
func startUpdate() {
self.locationManager.startUpdatingLocation()
}
enum PermissionStatus {
case unknown
case authorizedWhenInUse
case authorizedAlways
case restricted
case denied
}
func getLocation() -> CLLocationCoordinate2D? {
return self.location?.coordinate
}
}
在您看来:
struct TurnOnLocation: View {
var body: some View {
ZStack {
Color.orange
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Image(systemName: "globe")
.resizable()
.aspectRatio(contentMode: .fill)
.foregroundColor(.white)
Spacer()
Button(action: {
let locationManager = LocationManager()
locationManager.requestAuthorization()
// you should check for authorization result first
locationManager.startUpdatingLocation()
}) {
Text("Turn on location")
}.foregroundColor(Color.black)
.font(Font.system(size:22))
.padding(20)
.background(
Rectangle()
.fill(Color.white)
.border(Color.white, width:1)
)
.cornerRadius(50)
.shadow(radius: 10)
.padding(50)
}
}
}
}
要修复1秒后消失的问题,请尝试初始化位置管理,如下所示:
struct TurnOnLocation: View {
let locationManager = LocationManager() <- try to init location manager here
var body: some View {
ZStack {
Color.orange
.edgesIgnoringSafeArea(.all)
VStack {
Spacer()
Image(systemName: "globe")
.resizable()
.aspectRatio(contentMode: .fill)
.foregroundColor(.white)
Spacer()
Button(action: {
locationManager.locationManager.requestWhenInUseAuthorization()
locationManager.locationManager.startUpdatingLocation()
}) {
Text("Turn on location")
}.foregroundColor(Color.black)
.font(Font.system(size:22))
.padding(20)
.background(
Rectangle()
.fill(Color.white)
.border(Color.white, width:1)
)
.cornerRadius(50)
.shadow(radius: 10)
.padding(50)
}
}
}
}