2

I have been reading apple developer sources on how location updates work and reading tons stack overflow questions on it as well. Im trying to get my users location on my app all the time when the user put the app in the background. The code below is not my code, I found it from another stacker overflow source question and it is the only thing that gets me close to what i need. the code below works and updates me on the users location for 25 minutes only and then just stops, but what i want is it to work all the time as long as the app is in the background :(. The question was answered but I'm not quite understanding how this whole background stuff works. I've heard apple automatically terminates your app after 3 minutes or so, so anyway to bypass that would help. And also i do have my background modes checked as in Location updates and background fetch and even the info.plist that are needed as well which was said was suppose to automatically wake my app once terminated but that doesn't seem to work as well either. If anyone can help me this would be a tremendous help for my new app, it is the last bit of code i need aha, thank you in advance everyone. would also like to do it without adding a silent audio file so my app doesn't get rejected :)

override func viewDidLoad() {
    super.viewDidLoad()

    self.locationManager.requestAlwaysAuthorization()

    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.allowsBackgroundLocationUpdates = true

        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.startUpdatingLocation()

    var timer = NSTimer.scheduledTimerWithTimeInterval(5, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
}

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]){

    if UIApplication.sharedApplication().applicationState == .Active {

    } else {
        backgroundTaskIdentifier = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
            self.backgroundTimer.invalidate()
            self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval( 60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
        })
    }
}

func updateLocation() {

    var timeRemaining = UIApplication.sharedApplication().backgroundTimeRemaining

    print(timeRemaining)

    if timeRemaining > 60.0 {
            print("timeRemaining > 60.0")
        }
    } else {
        if timeRemaining == 0 {
          print("timeRemaining = 0")  UIApplication.sharedApplication().endBackgroundTask(backgroundTaskIdentifier)
        }

        backgroundTaskIdentifier2 = UIApplication.sharedApplication().beginBackgroundTaskWithExpirationHandler({ () -> Void in
            self.backgroundTimer.invalidate()
            self.backgroundTimer = NSTimer.scheduledTimerWithTimeInterval( 60.0, target: self, selector: "updateLocation", userInfo: nil, repeats: true)
        })
    }
}
Amen Parham
  • 73
  • 1
  • 8
  • Why do you want to do this? You can just update the user's location whenever he moves 500.0 m. Unless user moves that distance, his location will remain the same. – Rikh Mar 26 '17 at 16:27
  • My app requires to know where they are at all times for the main purpose of the app :) – Amen Parham Mar 26 '17 at 16:29

1 Answers1

3

You cannot (at present) run an app in the background continuously. That's how Apple intends things to work and you'd have to accept that. Once your app is in the background, it can be terminated at any point in time by the core OS.

Fahim
  • 3,466
  • 1
  • 12
  • 18
  • Any idea how instagram, snapchat etc send notifications when the user receives a dm or in snapchat a message? – Amen Parham Mar 26 '17 at 03:21
  • or even how the app MAPS given by apple can be ran in the background forever while in the background? – Amen Parham Mar 26 '17 at 03:22
  • 1
    Apple apps are (probably) exempt from the "Cannot run indefinitely in the background rule" - Apple can do whatever they want with their own apps :) The Instagram and Snapchat notifications probably are handled by a central server since the messages have to go through a server and the server will send out a push notification to the relevant user - not the same as continually running in the background. – Fahim Mar 26 '17 at 03:26
  • Thank you. Any Idea what these people are talking about then because they seem and other sites have seemed to figure this out? [here](http://stackoverflow.com/questions/19042894/periodic-ios-background-location-updates) not knocking what you have told me, just trying to look deeper into it :) – Amen Parham Mar 26 '17 at 03:32
  • What I'd suggest is that you try the code on that thread and see if it works for you - if it works, it works. But as far as I know, you can't have the app running *continuously* in the background - which is what your question was about :) – Fahim Mar 26 '17 at 03:36
  • @AmenParham .. how you did it ? – MAS. John Jun 28 '17 at 06:36
  • Google Maps would need the same exception. Also Waze, and many others. This cannot be the case. Too many "exceptions". –  Aug 31 '18 at 00:59