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
Tested on iOS 12.2 with Swift 5
Step 1. you must add following privacy permissions in plist file
NSLocationAlwaysAndWhenInUseUsageDescription
Application requires user’s location for better user experience.
NSLocationAlwaysUsageDescription
Application requires user’s location for better user experience.
NSLocationWhenInUseUsageDescription
Application requires user’s location for better user experience.
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)
}
}