0

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)
}

}

djt
  • 1
  • 5
  • How long do you wait after rebooting the phone? I have noticed that CoreLocation is not available immediately when the UI first comes up. I always wait 5 minutes before doing any testing. – davidgyoung May 21 '18 at 19:20
  • I am not opening APP .. Once I reboot I am just keeping my phone in car and start driving .. I am hoping that region did enter will trigger but it' not. – djt May 21 '18 at 19:40
  • Hey @davidgyoung I am not using backgroundRefersh option .. do i have to use it? – djt May 21 '18 at 20:13
  • You do not need that. I see no reason it would not work after a reboot – davidgyoung May 21 '18 at 23:43
  • thanks david, Is there any reference that you have will cross check once more. – djt May 22 '18 at 13:05
  • I do add more please check david – djt May 24 '18 at 15:27
  • I followed this link .. but not get any idea . Please explain .. https://stackoverflow.com/questions/23711378/ibeacon-region-monitoring-not-resumed-when-rebooting-the-phone-except-if-i-set – djt May 29 '18 at 14:06

1 Answers1

1

You must set up beacon monitoring and set the CoreLocation delegate in didFinishLaunching. If you do not, you will not get auto launching behavior.

var locationManager: CLLocationManager?  

func application(_ application: UIApplication,  didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
  locationManager = CLLocationManager()
  locationManager?.delegate = self
  let region = CLBeaconRegion(proximityUUID: Uuid, identifier: "my_region")
  locationManager?.startMonitoring(for: region)
}
davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • Oh k , I am doing when ever app enter background , Is there any way to setup monitoring in didFinishLaunching after phone got reboot or if I do that in didFinishLaunching do i have to in background again .. can you share me sample code how to do it in didFinishLaunching. – djt May 30 '18 at 14:38
  • if i do that in didFinishLaunching i don't have to do that in background call right? – djt May 30 '18 at 16:44
  • Correct, except you will need to change your delegate to point to where you want to get the callbacks – davidgyoung May 30 '18 at 20:57