Kodeco Forums

Background Modes Tutorial: Getting Started

Get your iOS Swift apps working with the most common background modes: audio playback, location updates, general tasks, and background fetch.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/890-background-modes-tutorial-getting-started

Has background location this app been tested in the real world with an iPhone. It works fine in the simulator, but absolutely does not track in background mode in real life. I’m running an iPhone 6 with iOS 10.1. Perfect in the simulator…failure in the iPhone out on the road.

Perhaps it is iOS 10…which wouldn’t surprise me. I’ve been trying for weeks to get iOS 10 to track in the background. I can make it work with third-party GPS devices from Dual, the 150 and 160 models, but it fails with Apple’s built-in GPS.

Have you verified that your App ID/Provisioning Profile supports Background Location Updates? Do you see the permission toggle under the Privacy Settings for it?

I tested this on device with iOS 10 Beta 5, the tutorial was completed before the final release of iOS 10. Let me know if you are able to see the privacy setting and I’ll verify that it is working on the most recent release version iOS 10.1.1

Thanks for the quick response.

I haven’t done anything to my App ID/Provisioning Profile, but in the Settings on the iPhone, Location is Always on, Background App Refresh is on but grayed out, and Cellular Data is on. Should these settings be sufficient?

And, I can see in the simulator that the tracking is working fine. You show the log of locations and in that I can see that latitude and longitude are changing. I am using the simulator’s Freeway Drive setting.

That should be sufficient, I think you’re probably good to go.

I just ran the app again and am seeing that the background updates are MUCH less frequent than foreground. I am suspecting you need to move around a bit more. If you leave the app backgrounded on your desk attached to the debugger for awhile you may see a few logs roll in.

I need to try this again actually moving around the neighborhood to confirm that it’s working as expected.

How much distance have you covered while testing this?

I have a 0.750 mile course that I routinely use to test my various GPS apps. Every time I traverse the course using the Dual 150 GPS receiver, a third-party device, it works correctly whether in foreground or background. Every time, no mistakes, no missing distance measures, no gaps in the breadcrumb tracking when the app is in background mode. When I turn off the Dual and run solely on Apple’s built-in GPS, the course also measures 0.750 miles, some times plus or minus about 0.010 miles, owing no doubt to sampling at only 1HZ rather than at 5HZ like the Dual 150. Then, whenever I put the app into background mode, the distance measurements cease. In other words, the app no longer updates the location parameters in the background. This is obvious when I return the app to the active mode. What I see is that on the next refresh the track is a straight line between the point where I entered background mode and the point where the app returned to the active mode. On your app, what I see is a gap in the pins displayed on the map view. Go into background and you get a gap every time with Apple’s built-in GPS. In short, background location tracking fails in iOS 10.1.1 whereas it has worked in iOS 9.3. It also works in the simulator. The problem that we may not be able to solve seems entirely of Apple’s making. I can’t get anyone at Apple to acknowledge this problem nor have I found anyone else who has mentioned this. I could have screwed up something in my apps, but I did nothing to yours. I don’t even code in Swift. I’m using your app straight up out of the box on an Apple iPhone 6 running iOS 10 in its three incarnations 10.0, 10.1, and 10.1.1.

When I run the app in the simulator, I use the debug location “Freeway Drive” that has a fairly uniform pace of about 75 miles an hour. On my test course in my neighborhood, I drive at between 15 and 25 miles per hour, not that this should matter.

My code is in Objective C. I’d be happy to send you the source code, if you’d like.

Yeah, feel free to put up on Dropbox or something and I’ll take a peak. I’m going to test this sample app out today as well.

Chris, my email address is mike@msyapps.com. Please send me yours and I’ll forward a .zip file of my GPS Rally Odometer app.

Hi Chris and thank you for this great tutorial. I have successfully applied the Background Location Updates part in my GPS tracking app. What I am trying to achieve is sending the GPS coordinates to Firebase every minute. When the app is in the foreground it works well. However, when in the background the coordinates are not sent to Firebase. Would you please point me in the right direction. I’ve been doing web development for 15 years but had just recently started coding in Swift for ios.

Thank you,
Martin

@cwagdev Great article. Thanks for that.

In my case, I actually translated every bit to Obj-C and I happened to have the same issue as @myoung.
It was working flawlessly in simulator, but not on device (both iOS10.1).
However, I found this other program that was using the location based mode (link: GitHub - voyage11/Location: Background Location Update Programming for iOS 7 and 8 over an extended period of time. Continue to get location on both foreground and background.) and I discovered two different properties of the location manager.

self.locManager.allowsBackgroundLocationUpdates = YES;
self.locManager.pausesLocationUpdatesAutomatically = NO;

After this, it was also working on device.

1 Like

@inigo333, oh! Thank you! I feel so bad that I haven’t had a chance to debug this for @myoung. I will reach out to him to make sure that he sees this. Thank you for sharing your findings.

All 5 of my GPS apps have these two lines of code in the - (void)viewDidLoad and it doesn’t cure the problem I’m having in iOS 10. To reiterate, the apps work perfectly in the simulator and in the real world with the Dual GPS Receiver, but not with Apple’s built-in GPS. So, strangely, the problem is isolated to Apple’s own GPS, not third-party devices. I can’t get anyone at Apple to address or even acknowledge the problem.

Hi,

I’m doing a VoIP app, so I’m using background mode.

Do you know if it is at all possible to change the content of the banner that is shown up top (and used to get back to the app) ?

Cheers,
Niklas

Hello,

I am trying to send data to my server after every one hour when the app is killed and suspended . is it possible to send data to server when the app is killed or suspended and when i ll get response from server i want to show local notification?

can u please lemme know is it possible this things ?

Cheers
Monk

I have a question for background fetch and please pardon my lack of knowledge in iOS, I have an app build in React Native, I add background fetch as capability, but my app stop calling the API after 3 minutes. I don’t know what else I have to do , or what should I follow to get it work.

My requirements are : call an API (took just a couple of sec) every 60 sec. even the app is in background/suspended when I move my app in background it runs for couple of minutes (3 minutes I believe) and stop after that.

Can you please guide me how I can achieve it?

Thank you

@nmodin I’ve never seen anyone do this and I don’t recall seeing any API for it, so I am going to assume that it’s not possible.

@monk, that does not fall under one of the background modes available. You could possibly send a silent push notification to request the information every hour?

@aamirmunir, does your app use one of the background modes mentioned? (e.g. VOIP, Music, GPS)