I am working on beacon detecting in foreground, background and kill (remove from background) app.I have kontakt
beacon.I have implemented location Manager delegate method but I can’t detect beacon in foreground and background not even called method didEnterRegion
and didExitRegion
.For solution I merge location manager delegate with KTKDevicesManagerDelegate. I can’t detect beacon in location manager so I have tried implementing in didRangeBeacons of location manager method I start devicesManager.startDevicesDiscovery()
of KTKDeviceManagerDelegate that time detect beacon in Foreground and background ,but I can’t detect beacon when we kill app. I have already added in Info.plist NSBluetoothPeripheralUsageDescription , NSLocationAlwaysUsageDescription , NSLocationWhenInUseUsageDescription , bluetooth-central , fetch , location.
I have added following on didFinishLaunchingWithOptions requestAlwaysAuthorization(),requestWhenInUseAuthorization()
.
//MARK:- Variable initializers
var devicesManager: KTKDevicesManager!
var locationManager = CLLocationManager()
let region = CLBeaconRegion(proximityUUID: NSUUID(uuidString: "********")! as UUID, identifier: "detected")
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
Kontakt.setAPIKey(“*******************”)
self.locationManager.delegate = self
devicesManager = KTKDevicesManager(delegate: self)
//When bluetooth Is on that time
devicesManager.startDevicesDiscovery(withInterval: 3)
}
//MARK: - KTKDevicesManagerDelegate Method
extension AppDelegate: KTKDevicesManagerDelegate {
func devicesManager(_ manager: KTKDevicesManager, didDiscover devices: [KTKNearbyDevice]) {
for device in nearbyDevices {
if let uniqueID = device.uniqueID {
print("Detected a beacon \(uniqueID)")
} else {
print("Detected a beacon with an unknown unique ID")
devicesManager.stopDevicesDiscovery()
}
}
}
func devicesManagerDidFail(toStartDiscovery manager: KTKDevicesManager, withError error: Error?) {
print("Discovery did fail with error: \(error)")
devicesManager.stopDevicesDiscovery()
}
}
//MARK: - Location Manager Method
extension AppDelegate: CLLocationManagerDelegate {
private func locationManager(manager: CLLocationManager!, didChangeAuthorizationStatus status: CLAuthorizationStatus) {
print("STATUS CHANGED: \(status.rawValue)")
if status == .authorizedAlways {
print("YAY! Authorized!")
locationManager.startMonitoring(for: region)
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
print("update location")
locationManager.startMonitoring(for: region)
locationManager.startRangingBeacons(in: region)
}
func locationManager(_ manager: CLLocationManager, didStartMonitoringFor region: CLRegion) {
print("The monitored regions are: \(manager.monitoredRegions)")
locationManager.startMonitoring(for: region)
locationManager.startRangingBeacons(in: region as! CLBeaconRegion)
}
func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
print("Location Manager failed with the following error: \(error)")
}
func locationManager(_ manager: CLLocationManager, didRangeBeacons beacons: [CLBeacon], in region: CLBeaconRegion) {
// print("beacons : \(beacons.count)")
devicesManager.startDevicesDiscovery()
//So called didDiscover method of KTKDevicesManagerDelegate method
}
func locationManager(_ manager: CLLocationManager, monitoringDidFailFor region: CLRegion?, withError error: Error) {
print("FAIL!")
}
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
print("Region Entered")
locationManager.startMonitoring(for: region)
locationManager.startRangingBeacons(in: region as! CLBeaconRegion)
}
func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
print("Region exited")
locationManager.startMonitoring(for: region)
locationManager.startRangingBeacons(in: region as! CLBeaconRegion)
}
}
You must add and allow location in
NSLocationAlwaysUsageDescription
.If you are in beacon region that timedidEnterRegion
method execute and out of beacon region that timedidExitRegion
method executed, that time we start and stop device discovery. we can able to detect beacon in foreground , background , lock phone and in kill state of application.Modify your method as given below.