Как реализовать iBeacon для фоновых изменений местоположения и когда aApp прекращается? - PullRequest
0 голосов
/ 07 мая 2018

Когда приложение инициализируется первым, я вызываю 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)


}}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...