gpt4 book ai didi

ios - startMonitoring 不适用于 "WhenInUseAuthorization"权限

转载 作者:行者123 更新时间:2023-11-28 06:23:15 25 4
gpt4 key购买 nike

我正在做一个项目,但是某个区域的监控功能不起作用。

所以我创建了一个非常简单的项目来测试这个功能。我发现了一个有趣的情况。

代码如下:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var map: MKMapView!

let clMan = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

map.showsUserLocation = true

clMan.requestWhenInUseAuthorization()
clMan.delegate = self
clMan.startUpdatingLocation()
clMan.startMonitoring(for:CLCircularRegion(center: CLLocationCoordinate2D.init(latitude: 51.5100888, longitude: -0.134575500000008), radius: 1000, identifier: "Picca")
)

}


func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print(region)
}
}

奇怪的是如果我改变

requestWhenInUseAuthorization()requestAlwaysAuthorization() 我的代码有效!

我将 Piccadilly Circus 设置为使用模拟位置进行测试。去伦敦之前定了另一个城市,后来又定了伦敦!

最佳答案

来自documentation :

If the user grants “when-in-use” authorization to your app, your app can start most (but not all) location services while it is in the foreground. (Apps cannot use any services that automatically relaunch the app, such as region monitoring or the significant location change service.

更多来自 documentation :

In iOS, registered regions persist between launches of your app. If a region boundary crossing occurs while your iOS app is not running, the system automatically wakes it up (or relaunches it) in the background so that it can process the event.

关于ios - startMonitoring 不适用于 "WhenInUseAuthorization"权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42749194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com