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

后端 未结 6 1195
慢半拍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条回答
  •  猫巷女王i
    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{
    

提交回复
热议问题