Location update even when app is killed/terminated
Asked Answered
L

1

10

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


}
Lobeline answered 30/12, 2015 at 10:47 Comment(17)
Possible Duplicate : #30396867Franciskus
"it's giving a red line on class AppDelegate" - that is on of the worst and least helpful error descriptions. Please post the error message here. If you ran your app via Xcode it is normal that the "red line" appears if you forcefully terminate the app.Adim
Yes i forcefully terminate the app and this red line error is shown but i am not getting location update on terminating app.What is the problem here??Lobeline
@Cade..this code is not working and i'm trying to find the solution!!!Lobeline
Could be great to know which line... and the error...Perplex
I have done some changes in code and edited my question.It's working fine in foreground but after few seconds it's not giving update location, even in background it's not calling didupdate location.But whwn i use startUpdatingLocation in place of stopMonitoringSignificantLocationChanges() it's working fine both in foreground and background and updating location frequently. Why is this happening??Lobeline
@Loïc Faure-Lacroix ..I it's not giving error now but i'm not sure whether location is updating in all states or not as while using stopMonitoringSignificantLocationChanges() , location is not updating frequently. Do you have any idea??Lobeline
Finally i got my work done..getting location update in all the states.There was problem in testing.startMonitoringSignificantLocationChanges() doesn't work fine with simulater in case of termination,so check it on device!! I have edited my code and it works fine :)Lobeline
can u put your answer here so that all can take a benifit of your codeDisprize
Yes sure!! I have post the updated code:)Lobeline
@KKb This code is working in iOS 9 after killing app? I didn't get it work from my side.Cleanshaven
YES!! it's working. "if #available(iOS 9.0, *) { self.locationManager!.allowsBackgroundLocationUpdates = true }"Have you put this line in your code??Lobeline
iOS 9 by default doesn't allow background location updates. You have to set it true.Lobeline
Not so sure on your variable spelling, but you code does work so thanks.Horseshoes
@Horseshoes ..You are welcome!! In case you have any doubt regarding the code feel free to ask me.Lobeline
Hi @Lobeline thanks for sharing your code :) . I want to update my server with the user's current location in every n minutes in all states of app. So would you please tell what changes I need to do here to get locations in every n minutes.Latinize
@Lobeline , May i know , whether you are able to get the current location of user even though the app is terminated or killed ?Doublet
M
2

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....

Motionless answered 1/12, 2017 at 16:45 Comment(5)
Hi did you find any success in making HTTP calls when app is in killed state?Zoroaster
Yes. I did. When the app state is in killed state. iOS controls the app when to wake up and do the location update call. But it definitely worked for me.Motionless
As per your blog you have mentioned the location manager updates once in 10 minutes. I know one app i.e. Life 360 that immediately updates the location as soon as user changes the location. Do you know how we can achieve that?Zoroaster
@Zoroaster You can use region monitoring.Petrol
@SaqibOmerI I was successful with getting location updates in killed state with location manager which worked for only half an hour and on significant changes my app was updating location.Zoroaster

© 2022 - 2024 — McMap. All rights reserved.