如何实现iBeacon后台位置更改以及何时终止应用程序?

时间:2018-05-07 17:42:58

标签: ios swift core-location cllocationmanager ibeacon

当应用程序首次初始化时,我调用startRegionMonitoring并符合位置管理器委托,因此当应用程序第一次启动时,didChangeAuthorization和didDetermineState被触发,然后我希望委托方法将通过位置更改而不是。

import Foundation

import CoreLocation

class DLLocationManager: NSObject {

var gatewayBridgeDelegate: DLGatewayBridgeDelegate?
private var locationManager:CLLocationManager
private var danlawRegion:CLBeaconRegion
override init() {
    danLogDebug("Starting up...")

    locationManager = CLLocationManager()
    danlawRegion = CLBeaconRegion(proximityUUID: uuid, identifier: "verify")  

    super.init()

    locationManager.delegate = self

    // required as "ALWAYS" for iBeacon
    locationManager.requestAlwaysAuthorization()

}
// Calling when the App is Initialized 

public func startRegionMonitoring()
{
    danLogDebug()
    // reset the regions...just in case
    //stopRegionMonitoring()

    // only add it if you need to
    if(!locationManager.monitoredRegions.contains(danlawRegion)) {
        locationManager.startMonitoring(for: danlawRegion)
    }

}

public func stopRegionMonitoring(){
    danLogDebug()
    locationManager.stopMonitoring(for: danlawRegion)
    resetRegions() // clear all regions...
}
//not using 
public func startLocationUpdates() {
    danLogDebug()
    // if using location updates...
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.activityType = .automotiveNavigation
    locationManager.distanceFilter = 1000
    locationManager.startMonitoringSignificantLocationChanges()
}
//not using
public func stopLocationUpdates(){
    danLogDebug()
    locationManager.stopMonitoringSignificantLocationChanges()
}

private func resetRegions(){
    danLogDebug()
    // maybe for development only
    for regions in locationManager.monitoredRegions {
        if(regions != danlawRegion) {
            locationManager.stopMonitoring(for: regions)
        }
    }
} }

//触发位置更改时委托方法

extension DLLocationManager: CLLocationManagerDelegate {

public func locationManager(_ manager: CLLocationManager, 
didChangeAuthorization status: CLAuthorizationStatus) {
    danLogDebug("")
    if status == .authorizedAlways || status == .authorizedWhenInUse {
        // JH: Removed to try and not use Location updates to conserve powerstartRegionMonitoring
        //manager.requestLocation()
    }
}

public func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
    danLogWarn(error.localizedDescription)
}

public func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    danLogDebug("")
   // onLocationUpdate()
}

public func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    danLogDebug("Region: \(region.identifier)")
    onRegionEnter(region: region.identifier)

}

public func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    danLogDebug("Region: \(region.identifier)")
    onRegionExit(region: region.identifier)

}

public func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {

    onRegionMonitorFail(region: region!.identifier, error: error.localizedDescription)
}
public func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {

    onRegionDetermineState(region: region.identifier, state: state.rawValue)
}
public func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
    danLogDebug("Region: \(region.identifier)")
    onRegionMonitorStart(region: region.identifier)


}}

0 个答案:

没有答案