【问题标题】:startMonitoring doesn't work with "WhenInUseAuthorization" permissionstartMonitoring 不适用于“WhenInUseAuthorization”权限
【发布时间】:2017-03-12 15:24:03
【问题描述】:

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

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

代码如下:

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 设置为使用该位置的模拟进行测试。 在去伦敦之前,我设置了另一个城市,然后是伦敦!

【问题讨论】:

    标签: ios swift mapkit core-location


    【解决方案1】:

    来自documentation

    如果用户向您的应用授予“使用时”授权,则您的应用可以在前台启动大多数(但不是全部)定位服务。 (应用不能使用任何自动重启应用的服务,例如区域监控或重大位置变更服务。

    更多来自documentation:

    在 iOS 中,注册区域会在您的应用启动之间保持不变。如果在您的 iOS 应用程序未运行时发生区域边界交叉,系统会在后台自动将其唤醒(或重新启动),以便它可以处理该事件。

    【讨论】:

    • 但是区域监控不会重新启动应用程序!但无论如何,谢谢你的回答。
    • 也许你可以提交一个雷达,让你在应用程序处于前台时监控区域。不过我不确定这会有多大用处。
    猜你喜欢
    • 2019-09-08
    • 1970-01-01
    • 2018-03-31
    • 1970-01-01
    • 2011-04-05
    • 2011-12-26
    • 1970-01-01
    • 2015-03-08
    • 2015-05-26
    相关资源
    最近更新 更多