iOS8: Blue bar “is Using Your Location” appears shortly after exiting app

后端 未结 6 1153
慢半拍i
慢半拍i 2020-12-25 10:40

I would like to get the blue bar when tracking in the background, but not when not.

My app uses location services all the time when active, so in iOS8 I use the

相关标签:
6条回答
  • 2020-12-25 11:06

    This is not a bug, you still have an active location manager somewhere in you app. Do you have a map view with showsUserLocation = YES for example? That could be it.

    I went over my project thoroughly and when i stopped all location managers the bar disappeared when it should.

    0 讨论(0)
  • 2020-12-25 11:07

    To show blue notification you need to add Privacy - Location When In Use Usage Description in Plist file (this is important, with Always Location the blue bar didn´t appear ever)

     self.locationManager.delegate = self;
     self.locationManager.requestWhenInUseAuthorization()
     self.locationManager.pausesLocationUpdatesAutomatically = true/false
     self.locationManager.allowsBackgroundLocationUpdates = true
    

    then star the location: locationManager.startUpdatingLocation()

    Also override methods:

     func locationManager(_ manager: CLLocationManager, didUpdateLocations
      locations: [CLLocation]) {
           print(manager.location?.coordinate.latitude ?? "No data")
     }
    
    
     func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
         if status == .authorizedWhenInUse {
             if CLLocationManager.isMonitoringAvailable(for: CLBeaconRegion.self) {
                 if CLLocationManager.isRangingAvailable() {
                     // do stuff
                     print(manager.location?.coordinate.latitude ?? "No data")
                     locationManager.startUpdatingLocation()
                 }
             }
         }
     }
    

    Remember the import!!:

     import CoreLocation
    

    And also remember the Delegate (CLLocationManagerDelegate):

     class ViewController: UIViewController, CLLocationManagerDelegate{
    
    0 讨论(0)
  • 2020-12-25 11:15

    We frequently report things to Apple, and sometimes they actually act upon it. Exactly to prevent the blue bar from appearing shortly as described in the question, Apple has introduced a new property on CLLocationManager in iOS-9. Set it at the moment you know you will need the location in the background:

    theLocationManager.allowsBackgroundLocationUpdates = YES;
    

    or, in a backward compatible way:

    if ([theLocationManager respondsToSelector:@selector(setAllowsBackgroundLocationUpdates:)]) {
        [theLocationManager setAllowsBackgroundLocationUpdates:YES];
    }
    

    in Swift:

    if #available(iOS 9.0, *) {
        theLocationManager.allowsBackgroundLocationUpdates = true 
    }
    

    If this property is not set, then the Blue Bar will not appear when exiting the app, and the user location is not available to your app. Note that in iOS 9, you must set the property in order to be able to use location in the background.

    See the WWDC video for Apple's explanation.

    0 讨论(0)
  • 2020-12-25 11:17

    If you follow steps below, Blue bar will not appear in background mode

    • set NSLocationAlwaysUsageDescription in Info.plist
    • check Capabilities > Background Modes > Location Updates
    • in code locationManager.requestAlwaysAuthorization()
    0 讨论(0)
  • 2020-12-25 11:23

    The blue bar only show when you enable Background Location Updates and request when-in-use authorization in iOS 8.

    Blue bar “is Using Your Location” appears shortly after exiting app

    Sounds like location manager can't stop immediately. So the blue bar will appear until location manager stop completely. Or maybe it's just a bug like Keith said.

    0 讨论(0)
  • 2020-12-25 11:25

    To show blue bar on header that your app is using location when pressing home button you have to set Privacy - Location When In Use Usage Description in Plist file

     self.locationManager.delegate = self;
    
     locationManager.desiredAccuracy = kCLLocationAccuracyBest
    
     self.locationManager.requestWhenInUseAuthorization()
    
     self.locationManager.pausesLocationUpdatesAutomatically = true
    
     self.locationManager.allowsBackgroundLocationUpdates = true
    

    when you use self.locationManager.requestAlwaysAuthorization() then it will not display blue header

    you need only use self.locationManager.requestWhenInUseAuthorization() then it will display blue header when you app is in background that your app is using Location

    you have to also set background mode in capabilities in target for location

    0 讨论(0)
提交回复
热议问题