Silver Challenge *using* MKMapViewDelegate


Ok, here’s how I did it and it seems pretty simple in hindsight.

Created and strong reference to CLLocationManager

var mapView: MKMapView!
var locationManager: CLLocationManager!

Added self as the delegate and instantiate the CLLocationManager()

override func loadView() {
        mapView = MKMapView()
        mapView.delegate = self
        locationManager = CLLocationManager()

Created the button in code with constraints

        let showLocButton = UIButton(type: .System)
        showLocButton.setTitle("Show Loc", forState: .Normal)
        showLocButton.translatesAutoresizingMaskIntoConstraints = false

        showLocButton.addTarget(self, action: "showLocButton:", forControlEvents: .TouchUpInside)
        let topButtonConstraint = showLocButton.topAnchor.constraintEqualToAnchor(segmentedControl.bottomAnchor, constant: 8)
        let leadingButtonConstraint = showLocButton.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)
        let trailingButtonConstraint = showLocButton.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor) = true = true = true

Implemented the action: for the button

func showLocButton(sender: UIButton!) {
        mapView.showsUserLocation = true

I added the NSLocationWhenInUseUsageDescription in the info.plist

Implemented the delegate

func mapView(mapView: MKMapView, didUpdateUserLocation userLocation: MKUserLocation) {
        let zoomedInCurrentLocation = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 500, 500)
        mapView.setRegion(zoomedInCurrentLocation, animated: true)

All in, 20? lines of code and most of it was the button.


This worked for me but. What I need to first was make class MapViewController conform to MKMapViewDelegate and CLLocationManager

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

Got that line from a video also posted in this forum.


This code works nicely! However, I missed the original posters line to add a new key to the plist. So upon hitting the Show Location Button, the state of the map remained unchanged and the following message appeared in the console…

2016-10-01 11:19:24.300 WorldTrotter06 This app has attempted to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSLocationWhenInUseUsageDescription key with a string value explaining to the user how the app uses this data

Here’s the procedure to add the necessary key to the plist if you don’t know how.

  1. Select Info.plist from Project Navigator
  2. Hover your cursor over one the the items in ‘Information Property List’ and click the ‘+’ button.
  3. In the drop down menu select the key: ‘Privacy - Location When In Use Usage Description’ (note: other comparable keys would also work)
  4. In the corresponding ‘value’ area you can enter an additional string which’ll appear in the iOS Action Message.
  5. Run project again

Another caveat; this app should be tested on an iOS device instead of the simulator. This is probably true with any app using CLLocationManager. My simulator keeps pointing me to the Apple Store in Union Square, SF.


Great Video on obtaining User location and author provides an indepth explanation of what data you can gather & track from users.