John Ramos John Ramos - 6 months ago 60
Swift Question

Background location tracking not working on physical device swift

I followed this tutorial (https://www.raywenderlich.com/92428/background-modes-ios-swift-tutorial) to create an app that maps the user's location in the background. It asks to follow the user's location even when the app is closed, the user presses yes, and it tracks location in the background, updating every second or so. It works perfectly in the simulator, but sideloaded onto a physical iPhone 6, it stops checking the user's location in the background.

Answer

I solved this. If your background location tracking is working in the simulator but not on a physical phone running iOS 9 or later, it is fixed by adding this code:

if #available(iOS 9.0, *) {
            locationManager.allowsBackgroundLocationUpdates = true
        } else {
            // You don't need anything else on earlier versions.
        }

Put this anywhere, maybe in your viewdidload.

Comments