I try to get location updates even in all states, even when the application is killed / terminated / paused. I turned on the background selection in xcode and implemented the following code (using the Capture Location link in all state applications ). But when I complete the application, it gives a red line in the AppDelegate class. I do not understand what the problem is. I did this using the solution to the question " Getting space for an iOS application when it is in the background and even killed " here, but it does not work in ios 9. Please help me or tell me another solution.
UPDATED CODE -
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate { var window: UIWindow? var locationManager: CLLocationManager? var significatLocationManager : CLLocationManager? func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject : AnyObject]?) -> Bool { if(UIApplication.sharedApplication().backgroundRefreshStatus == UIBackgroundRefreshStatus.Available){ print("yessssss") }else{ print("noooooo") } if let launchOpt = launchOptions{ if (launchOpt[UIApplicationLaunchOptionsLocationKey] != nil) { self.significatLocationManager = CLLocationManager() self.significatLocationManager?.delegate = self self.significatLocationManager?.requestAlwaysAuthorization() if #available(iOS 9.0, *) { self.significatLocationManager!.allowsBackgroundLocationUpdates = true } self.significatLocationManager?.startMonitoringSignificantLocationChanges() }else{ self.locationManager = CLLocationManager() self.locationManager?.delegate = self self.locationManager?.requestAlwaysAuthorization() if #available(iOS 9.0, *) { self.locationManager!.allowsBackgroundLocationUpdates = true } self.locationManager?.startMonitoringSignificantLocationChanges() } }else{ self.locationManager = CLLocationManager() self.locationManager?.delegate = self self.locationManager?.requestAlwaysAuthorization() if #available(iOS 9.0, *) { self.locationManager!.allowsBackgroundLocationUpdates = true } self.locationManager?.startMonitoringSignificantLocationChanges() } return true } func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){ let locationArray = locations as NSArray let locationObj = locationArray.lastObject as! CLLocation let coord = locationObj.coordinate } func applicationDidEnterBackground(application: UIApplication) { if self.significatLocationManager != nil { self.significatLocationManager?.startMonitoringSignificantLocationChanges() }else{ self.locationManager?.startMonitoringSignificantLocationChanges() } }
Kirti source share