My app is working fine for beacon monitoring when running in foreground and background even when I quit app it's launching the app in background .. But when I restart the phone it's not waking up..
// I am using background modes for location also
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
//do i have to do anything here
return true
}
//when ever app hit background bellow method will trigger and there i am starting region monitoring
func applicationDidEnterBackground(_ application: UIApplication) {
do {
try DLGatewayInterface.getInstance().enableBackgroundWakeups()
try DLGatewayInterface.getInstance().startBackgroundScan(start: true)
} catch let error {
print(error)
}
}
// This will trigger when ever app starts region monitoring here i am setting up delegate
var gatewayBridgeDelegate: DLGatewayBridgeDelegate?
private var locationManager:CLLocationManager
private var Region:CLBeaconRegion
var savedDevices = UserDefaults.standard
var major = ""
var minor = ""
override init() {
locationManager = CLLocationManager()
super.init()
locationManager.delegate = self
// required as "ALWAYS" for iBeacon
locationManager.requestAlwaysAuthorization()
}
// this will trigger every time when app hits background
public func startRegionMonitoring()
{
if let beaconName = UserDefaults.standard.value(forKey: "beaconDeviceRegion") as? String{
print(beaconName)
self.decimalToHexForMajorMinor(value: beaconName)
danlawRegion = CLBeaconRegion(
proximityUUID: BeaconServiceId,
major: CLBeaconMajorValue(DLUtils.beaconMajor),
minor: CLBeaconMinorValue(DLUtils.beaconMinor),
identifier: "Danlaw")
// reset the regions...just in case
stopRegionMonitoring()
// only add it if you need to
if(!locationManager.monitoredRegions.contains(Region)) {
locationManager.startMonitoring(for: Region)
}
}
}
public func stopRegionMonitoring(){
danLogDebug()
locationManager.stopMonitoring(for: danlawRegion)
resetRegions() // clear all regions...
}
extension DLLocationManager: CLLocationManagerDelegate {
public func locationManager(_ manager: CLLocationManager,
didChangeAuthorization status: CLAuthorizationStatus) {
if status == .authorizedAlways || status == .authorizedWhenInUse {
}
}
public func locationManager(_ manager: CLLocationManager,
didFailWithError error: Error) {
danLogWarn(error.localizedDescription)
}
public func locationManager(_ manager: CLLocationManager,
didUpdateLocations locations: [CLLocation]) {
}
public func locationManager(_ manager: CLLocationManager,
didEnterRegion region: CLRegion) {
// in this bellow method i am calling scanning again
onRegionEnter(region: region.identifier)
}
public func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
onRegionExit(region: region.identifier)
}
public func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
}
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) {
onRegionMonitorStart(region: region.identifier)
}
}