Location Services not working in iOS 11

前端 未结 8 559
春和景丽
春和景丽 2020-11-27 14:59

I just rebuilt my app with the iOS 11 SDK in an attempt to remove the blue banner that is now always appearing. I thought - \"Brilliant, that worked\", only to

相关标签:
8条回答
  • 2020-11-27 15:38

    Follow these steps:

    I ran into the same issue with an app that needed "Always Authorization", and resolved it by following these steps:

    1. Add NSLocationWhenInUseUsageDescription key to Info.plist

    2. Add NSLocationAlwaysAndWhenInUseUsageDescription to Info.plist

    3. Add NSLocationAlwaysUsageDescription to Info.plist (to support < iOS 11)

    4. Call requestWhenInUseAuthorization() BEFORE requestAlwaysAuthorization()

    You cannot execute requestAlwaysAuthorization() before requestWhenInUseAuthorization(). You must escalate to that permission level. Once I made these changes, location updates started working properly again.

    More details can be found here:

    https://developer.apple.com/documentation/corelocation/choosing_the_authorization_level_for_location_services/requesting_always_authorization

    0 讨论(0)
  • 2020-11-27 15:39

    Tested on iOS 12.2 with Swift 5

    Step 1. you must add following privacy permissions in plist file

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>Application requires user’s location for better user experience.</string>
    
    <key>NSLocationAlwaysUsageDescription</key>
    <string>Application requires user’s location for better user experience.</string>
    
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>Application requires user’s location for better user experience.</string>
    

    Step 2. make sure you have following swift code to get current locations

    import UIKit
    import MapKit
    import CoreLocation
    
    class ViewController: UIViewController, CLLocationManagerDelegate {
    
        // MARK: Variables declearations
        @IBOutlet weak var mapView: MKMapView!
        var locationManager: CLLocationManager!
    
        // MARK: View Controller life cycle methods
        override func viewDidLoad() {
            super.viewDidLoad()
            //TODO: Make user you must add following three privacy permissions in plist
            //NSLocationWhenInUseUsageDescription
            //NSLocationAlwaysAndWhenInUseUsageDescription
            //NSLocationAlwaysUsageDescription
    
            getCurrentLocation()
        }
    
        func getCurrentLocation()
        {
            if (CLLocationManager.locationServicesEnabled())
            {
                locationManager = CLLocationManager()
                locationManager.delegate = self
                locationManager.desiredAccuracy = kCLLocationAccuracyBest
                locationManager.requestAlwaysAuthorization()
                locationManager.startUpdatingLocation()
            }
        }
    
        // MARK: Location Manager Delegate methods
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
        {
            let locationsObj = locations.last! as CLLocation
            print("Current location lat-long is = \(locationsObj.coordinate.latitude) \(locationsObj.coordinate.longitude)")
            showOnMap(location: locationsObj)
        }
        func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
            print("Get Location failed")
        }
    
        func showOnMap(location: CLLocation )
        {
            let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
            let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
            mapView.setRegion(region, animated: true)
        }
    }
    
    0 讨论(0)
提交回复
热议问题