Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

startMonitoringSignificantLocationChanges not working in swift

I'd added CLLocationManager in my app using Swift in the AppDelegate file.

In the Appdelegate.swift file,

import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var locationManager: CLLocationManager!

In the didbecomeActive method:

func applicationDidBecomeActive(application: UIApplication) {
    if((locationManager) != nil)
    {
        locationManager.stopMonitoringSignificantLocationChanges()
        locationManager.delegate = nil
        locationManager = nil
    }
    locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.activityType = CLActivityType.OtherNavigation
    locationManager.requestAlwaysAuthorization()
    locationManager.startMonitoringSignificantLocationChanges()
}

If I use startUpdatingLocation, didUpdateLocations method gets called, but not for startMonitoringSignificantLocationChanges.

Why it's not getting called for startMonitoringSignificantLocationChanges. I'm testing this in ios simulator. I don't know how to check in device.

like image 552
Nazik Avatar asked Dec 15 '15 10:12

Nazik


3 Answers

In your simulator, select Features -> Location -> Freeway Drive

Wait a bit for startMonitoringSignificantLocationChanges to trigger didUpdateLocations.

like image 101
Riajur Rahman Avatar answered Oct 22 '22 23:10

Riajur Rahman


It's working, but it's really hard to trigger significant location changes - it usually happens when the device is changing cell towers - I don't think it's possible to do with the simulator.

You'd probably have to get on a bike/car and travel AT LEAST a few kilometres.

There's a trick you can use though, that will trigger significant location change:

Switch Airplaine mode in your iPhone on and off with a few second intervals repeatedly, it should trick the device into thinking that it changed cell towers and trigger the significant location change.

enter image description here

like image 30
michal.ciurus Avatar answered Oct 22 '22 23:10

michal.ciurus


In your simulator, goto Debug->Location->Custom and change location, then test it.

like image 3
Rujoota Shah Avatar answered Oct 22 '22 23:10

Rujoota Shah