加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

找出statUpdatingLocation在Swift中是否处于活动状态

发布时间:2020-12-14 04:36:50 所属栏目:百科 来源:网络整理
导读:我有一个基于位置的应用程序在 swift中运行.我试图检测self.locationManager.startUpdatingLocation()当前是否处于活动状态. 我正在努力寻找如何做到这一点,我也无法在互联网上找到它.我相当肯定这很容易实现.我不想设置BOOL,因为它需要是全局的. if CLLocat
我有一个基于位置的应用程序在 swift中运行.我试图检测self.locationManager.startUpdatingLocation()当前是否处于活动状态.

我正在努力寻找如何做到这一点,我也无法在互联网上找到它.我相当肯定这很容易实现.我不想设置BOOL,因为它需要是全局的.

if CLLocationManager.locationServicesEnabled() && /* START UPDATE LOCATION GOES HERE */ {

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestAlwaysAuthorization()
        self.locationManager.startUpdatingLocation()

        //self.locationManager.startMonitoringSignificantLocationChanges()

        sender.setTitle("END DAY",forState: UIControlState.Normal)

    } else {


    }

解决方法

我知道现在这是一个迟到的答案.但是,如果您想知道locationManager是否正在更新,这可能是一种解决方案.

您的应用中应该只有一个CLLocationManager实例.因此,创建单身人士是理想的选择.然后,您应该重写方法startUpdatingLocation和stopUpdatingLocation.

(斯威夫特3)

import CoreLocation

class LocationManager: CLLocationManager {
    var isUpdatingLocation = false

    static let shared = LocationManager()

    override func startUpdatingLocation() {
        super.startUpdatingLocation()

        isUpdatingLocation = true
    }

    override func stopUpdatingLocation() {
        super.stopUpdatingLocation()

        isUpdatingLocation = false
    }
}

用法:

if LocationManager.shared.isUpdatingLocation {
    print("Is currently updating location.")
  } else {
    print("Location updates have stopped.")
  }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读