mapzen / ios

Where you can find everything iOS from Mapzen
https://mapzen.com/projects/mobile/
Apache License 2.0
40 stars 24 forks source link

NSInternalInconsistencyException in CLLocation Manager setAllowsBackgroundLocationUpdates #393

Closed msmollin closed 6 years ago

msmollin commented 6 years ago

Description

Reported by @nolispe280 in #390 -

"I have managed to load the SDK 1.1.1 via cocoapods (had to uninstall and reinstall it). I then added this code in the viewDidLoad function:

if self.locationManager.canEnableBackgroundLocationUpdates() {
        _ = self.locationManager.enableBackgroundLocationUpdates(forType: .other, desiredAccuracy: kCLLocationAccuracyBest, pausesLocationAutomatically: false)
      }

but still get the above error. Does anyone else have this issue?"

msmollin commented 6 years ago

I'll try to replicate this issue today.

Can you post what version of iOS you're testing on?

nolispe280 commented 6 years ago

Great! I'm on iOS 11 coding on XCode 8

msmollin commented 6 years ago

How are you deploying to iOS 11 with Xcode 8? Xcode 8 doesn't include the necessary symbols to deploy to iOS 11 devices.

nolispe280 commented 6 years ago

Sorry I meant iOS 10.3.3 coding on Xcode 8.

msmollin commented 6 years ago

Tested this scenario in a sample project and did not run into the issue mentioned.

However doing some research on it, that issue can arise when a developer doesn't add the correct plist entries for background modes and location privacy keys. I would double check your plist entries and make sure everything is set up correctly.

This also caused me to double check our sample app and noticed that technically our sample app is iOS 10 incompatible, so I opened #394 to track that.

Below is the code I used to test it. Perhaps it will give you some insight into what you're facing:

import UIKit
import Mapzen_ios_sdk
import CoreLocation

class ViewController: MZMapViewController {
  override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    locationManager.requestAlwaysAuthorization()

  }

  override func authorizationDidSucceed() {
    super.authorizationDidSucceed()
    if locationManager.canEnableBackgroundLocationUpdates() {
      _ = locationManager.enableBackgroundLocationUpdates(forType: .other, desiredAccuracy: kCLLocationAccuracyBest, pausesLocationAutomatically: false)
    }
  }

  //Not necessary to override - only added so we can see this update in the background.
  override func locationDidUpdate(_ location: CLLocation) {
    print("Location did update!")
    super.locationDidUpdate(location)
  }
}

You'll also want to make sure you set your API key in your app delegate.

nolispe280 commented 6 years ago

Indeed I needed to set the "Required Background Modes" to "location" in my plist file! But FYI, this is the first time I hear of this entry. Thanks anyway!