24

Starting from iOS 12 the CLLocationManager doesn't run endless anymore in the background. The app get's terminated without a crashlog at random times. This was working fine before iOS 12.

To demonstrate this problem I've created an sample app here

The demo app just launches an CLLocationManager and keep this running in the background. While running on the background we keep track of it by logging it. The problem is that the app get terminated by iOS. The demo app is created to demonstrate this problem.

Steps to reproduce

  1. Run the app on the device
  2. Grand access to locationmanager
  3. Put the app to the background
  4. Wait for 1-48hours

Result:

  1. The app is terminated

App is terminated without any reason after random time.

Expected result:

  1. The app is still running.

How it should work

This is confirmed by an Apple engineer:

Once the CLLocationManager updates are started in the foreground and you did all the work to have it running in the background, the location updates should run endless in the background until:

  • app is force quit
  • device is rebooted
  • app stops location updates
  • app releases the CLLocationManager object
  • app crashes
  • iOS terminates the app due to memory shortage,
  • the locationManager object is released, overreleased, or overwritten. You should make sure that your view controller is not being instantiated, which then resets the locationController class. If that happens when the app is in the background, you will restart updates, which will cause the app to be eventually suspended. You should make sure the locationController is a singleton.
  • app is crashing. Check to see if there are crash logs on the device you are testing
  • iOS is terminating the app due to memory shortage. In this case, you will find JetsamEvent logs on the device that shows your app being terminated. You can check the timestamps and locate the one that is around the time your app stopped working.
Sjoerd Perfors
  • 2,317
  • 22
  • 37

2 Answers2

28

Updated answer:

Apple fixed this bug in iOS 12.2 beta 2 (16E5191d)

Original analyse and bug detection:

Together with Apple Developer Technical Support we have analyzed this issue with Sysdiagnose files. Following these guidelines you can install profiles to have more logging on your device. I don't know exactly how these logs work and where to find this issue, but Apple did this for me and came with this first analyse:

On the suspension event you observed on 2018/10/22 01:01:12:587, this is what I see (about a minute after your last activity logging)

[CllocationManag:2725] Terminating with description: { owner = ; target = rw.sp.flitsmeister.frameworks.CllocationManagerBackgroundTest; }

This is basically saing that your app was terminated, because the system needed drive space, and killed a bunch of apps so it can delete their /tmp and /Library/Caches directories. I have seen this process to be a little more aggressive in iOS 12, but seeing you are on a 256GB device, and have ~179 GB free after the cleanup, I am finding it hard to believe this was justified.

After sending some more sysdiagnose and reproduction cases Apple did his best to analyse and ended with the following conclusion:

Unfortunately I don’t bring good news.

It turned out that, currently in iOS 12 there is a new mechanism that will terminate long running background apps periodically as the system needs to free resources. At this time, this process is a bit too aggressive, and I am working with the relevant teams to get this to behave better.

So, at this point, I would like you to file a bug report. Explain the symptoms. And make sure you upload the sysdiagnose files along with the bug report. (I already sent yours in, but it doesn’t hurt to have the new ones as well). And let me know the bug number please.

So this means that currently in iOS 12 you app won't run endless on the background. I've filled in a bug report, the number is 45581276 and will try to keep this thread updated.

Sjoerd Perfors
  • 2,317
  • 22
  • 37
  • 3
    I am facing the similar issue in long run of application in background. Any updates from Apple? – Jas_meet Oct 31 '18 at 05:21
  • 2
    So finally I found this on SO. I have this issue since ios 12 beta. I thought this will be resolved when release comes out. Anyways I will keep looking it here as your bug doesn't seem to be on openradar. – Naresh Oct 31 '18 at 07:33
  • 3
    Just added this on open radar as well – Jas_meet Oct 31 '18 at 09:01
  • @Jas_meet did you have a link for the open radar? – xizor Jan 03 '19 at 15:02
  • @SjoerdPerfors any update from Apple, are you still seeing this and did you find a workaround? – xizor Jan 03 '19 at 15:03
  • 2
    This IOS12 behaviour change has effectively destroyed my app. Android did something similar with the introduction of Android P a year or so ago, but up until IOS 12, my app would work almost forever in the background, now it does not. I am at a loss as to what to do. Is it possible to view progress of Apple Bug Reports? – Steve Brooker Jan 04 '19 at 07:32
  • No change in new iOS Version 12.1.1. My background app terminated after about 16 hours. – Michael Konz Jan 08 '19 at 13:14
  • isn't there any workaround for this? at least till apple devs fix this issue – Nick Cuypers Jan 09 '19 at 08:04
  • 1
    The only workaround I can see is to let the app run in the foreground and disable the lock screen. Thus increasing battery drain considerably. And of course that might not be a viable option for most apps. – Michael Konz Jan 09 '19 at 13:43
  • We are also suffering from the same problem with an App on iOS 12.+ running with LocationManager in the Background. We have made a bug report to Apple (47171720) and referenced this Post and Sjoerd Perfors's Apple Bug Report (45581276). A few days later our bug report was marked as a duplicate of 45505724. – chiwi Jan 14 '19 at 13:21
  • @SjoerdPerfors : I was wondering if you have implemented applicationDidReceiveMemoryWarning and if iOS is calling it or not? We have not implemented it in our app. As you stated in your answer the problem can't really be due to memory pressure. But it would still be interesting it know if this method is being called. Perhaps apps which do not implement it are penalised in some way? – chiwi Jan 15 '19 at 06:26
  • 1
    We've implemented this methode `applicationDidReceiveMemoryWarning ` and it did not get called around the time the app will be suspended. The implementation we did was a simple log message to disk. – Sjoerd Perfors Jan 15 '19 at 10:15
  • Same here, this method never gets called. So up until now there is no way to monitor when your app get's killed by the OS and due to what reason... – Nick Cuypers Jan 21 '19 at 08:41
  • I am also working on a location based app which runs continuously in background but it will crash only after i will start charing phone and device remains idle for a while. Has anyone been noticing similar behaviour? https://stackoverflow.com/questions/52751712/all-applications-are-terminated-by-ios-12-when-iphone-is-charging – Puneet Sharma Jan 22 '19 at 06:34
  • 1
    @PuneetSharma I have also noticed some relationship to charging the device. I have not yet been able to notice any pattern or replicate the issue reliably. – chiwi Jan 24 '19 at 06:19
  • 1
    Try testing it with iOS 12.2 beta 2 (16E5191d). Should be fixed. – Sjoerd Perfors Feb 05 '19 at 10:15
  • @SjoerdPerfors Did you hear something from Apple regarding a fix? There is no mention in the release notes: https://developer.apple.com/documentation/ios_release_notes/ios_12_2_beta_2_release_notes – chiwi Feb 13 '19 at 06:36
  • 1
    Yes I saw it Fixed, our app stayed at least 4 days in the background – Sjoerd Perfors Feb 19 '19 at 08:27
  • 1
    I’ve been testing it and have had it run continually in the background without being terminated. Finally got the closed as fixed message in radar yesterday. – xizor Feb 28 '19 at 02:44
  • I am having the same issue with our app which uses Location Svs and Core Bluetooth. As of 12.2 (16E227) there is no change - the app continues to get shutdown when running in the Background. – Bassman May 06 '19 at 19:59
3

I tried clearing out the /tmp and /Library/Caches directories when the app goes in the background without any change in behavior. I also tried the same periodically when the app processing background locations without any change as well.

Scott
  • 31
  • 4
  • Best thing to do is contact Developer Technical Support by submitting a question. The decision makers that can make a change do not appear to be taking any action from the information I am receiving. Impress on them it is negatively effecting your users and you need the changes in iOS 12 to be rolled back. – Scott Nov 19 '18 at 19:40
  • I don't understand what you're posting here @Scott ? This is actually what I did right? – Sjoerd Perfors Nov 20 '18 at 13:38
  • Sorry Sjoerd, you did the right thing. I saw some others submitting a bug to Apple's Bug Reporter. I am guessing you and I are in contact with the same support engineer. This issue has been a nightmare for me. – Scott Nov 20 '18 at 20:43
  • Hey guys, I have the same issue. Just keep us posted if there are any updates from Apple. – Naresh Jan 11 '19 at 10:32
  • This has been fixed in 12.2 beta 2 – Scott Feb 07 '19 at 21:15
  • @Scott I still see this same issue happening, any idea how to fix this? – Puneet Sharma Aug 08 '19 at 02:51