Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Location update even when app is killed/terminated

I am trying to get location updates even in all states, even when app is killed/terminated/suspended. I have enabled background fetch in xcode and implemented the following code(used reference "Capture location in all states app"). But when i terminate the app it's giving a red line on class AppDelegate. I do not understand what is the problem here.I have done this using the solution of the question "Getting location for an iOS app when it is in the background and even killed" here, But its not working in ios 9.Please help me out or tell me the other 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()
    }


}
like image 355
Kirti Avatar asked Dec 30 '15 10:12

Kirti


People also ask

What is background location?

Android 10 features a background access location reminder, which increases transparency into how much access apps have to a device's location and helps users maintain control over such access. In Android 9 and lower, an app can track a device's location while running in the background without the user's knowledge.

How do I keep apps from running in the background in Swift?

First, go to your project's settings and choose the Capabilities tab. You need to enable the Background Modes capability, then check the box marked Background Fetch. This modifies your app's Info.

How do I change the background location in Swift?

Location Updates in Background ModeGo the Application target → Sign in & Capabilities → Capabilities button on the left → Choose Background Modes. Then, enable location updates.


1 Answers

Check this tutorial: http://mobileoop.com/getting-location-updates-for-ios-7-and-8-when-the-app-is-killedterminatedsuspended

And the source code here: https://github.com/voyage11/GettingLocationWhenSuspended

Hope you will get your answer. It's working for me. Now I am trying to make a http request when 'didUpdateLocations' function being called, so that I can store user current location in server when the app is not running(Suspended/terminated/killed).

I don't think apple allowed to make any http request when the app is suspended/terminated. But if server received the request of location update, then I am good to go with that. Because I don't need response back from server. Need a lot of testing....

like image 135
A K M Saleh Sultan Avatar answered Oct 01 '22 08:10

A K M Saleh Sultan