Beginning iOS Development

Course No. 10-152-143

Chapter 5 — MapKit and Text Input (iOS)

Object Diagrams

  • If you have the PDF version of the book then the object diagram is not very good. Here’s a better version.

MapKit Framework

  • You can create the properties by typing them or see below and have Xcode create them for you.

Interface Properties

  • Click on the WhereamiViewController.xib file. Then Option-click on the WhereamiViewController.m to open it in a second editor window.
  • Hold the Control key, click and drag from the three UI objects to the code and create the outlet properties.

Connect the Delegate

  • Control-click on the MKMapView in Interface Builder. This will open a dialog box.

  • Click and hold on the circle icon to the right of the word delegate. Drag over to the File’s Owner and let go.

  • This will connect the delegate and the icon will show a dot in the center of the circle.

  • Repeat this for the UITextField object.

Checking Connections

  • To check all the connections, Control-click on the File’s Owner. This will bring a dialog that should look like this.

  • If yours looks like that then you are all wired up and ready to code.

Customizing the UI

  • Click on the UITextField and customize it like this.

  • Click on the UIActivityIndicatorView and customize it like this.

Being a MapView Delegate

  • The new way to code the view viewDidLoad method.
  • Delete this line from initWithNibName:bundle:
  • Add the Map View Delegete protocol to WhereamiViewController.h file.

Run it!

  • First, let’s run it in the simulator.

  • You can change the location so someplace like London, but the view won’t scroll. You can manually scroll there. We’ll fix this soon.

  • Now run it on your device. It should show where you really are!

  • You can zoom in.

Using the documentation

  • Let’s look at the documentation again.
  • Option-click on something that’s part of an Apple framework. Then click the link on the bottom of the popup window.

  • Let’s look that the delegate mehtods.
  • Tasks
  • Responding to Map Position Changes
    • – mapView:regionWillChangeAnimated:
    • – mapView:regionDidChangeAnimated:
  • Loading the Map Data
    • – mapViewWillStartLoadingMap:
    • – mapViewDidFinishLoadingMap:
    • – mapViewDidFailLoadingMap:withError:
  • Tracking the User Location
    • – mapViewWillStartLocatingUser:
    • – mapViewDidStopLocatingUser:
    • – mapView:didUpdateUserLocation:
    • – mapView:didFailToLocateUserWithError:
    • – mapView:didChangeUserTrackingMode:animated: required method
  • Managing Annotation Views
    • – mapView:viewForAnnotation:
    • – mapView:didAddAnnotationViews:
    • – mapView:annotationView:calloutAccessoryControlTapped:
  • Dragging an Annotation View
    • – mapView:annotationView:didChangeDragState:fromOldState:
  • Selecting Annotation Views
    • – mapView:didSelectAnnotationView:
    • – mapView:didDeselectAnnotationView:
  • Managing Overlay Views
    • – mapView:viewForOverlay:
    • – mapView:didAddOverlayViews:
  • Notice what’s changed since the book was published? There’s a new required method. However, we just ran the app and it didn’t complain that we haven’t implemented this method. Why?
  • We’re going to implement one of the other methods.
  • Next we need to find the property that will tell us where to zoom. It is called region and is type MKCoordinateRegion.
  • MKCoordinateRegion is a C struct. (Knowing about C structs is very handy!)
  • Parts of a region

  • The finished mapView:didUpdateUserLocation: method updated for the course.
  • Important note from the book!
    • “You cannot be an iOS developer without using the API Reference.”

Your own MKAnnotation

  • A Map Annotation is when we drop a pin at a location. We do it with the MKAnnotation protocol.
  • Here is the header file for the protocol MKAnnotation.
  • When you create the new BNRMapPoint class here are the changes for Modern Objective C.
    • The properties should still be in the .h file as in the book. They are public properties.
    • The initWithCoordinate:title: will also be in the .h file, it needs to be a public method.
    • No need to @synthesize the properties.
    • The initWithCoordinate:title: method implementation.
  • The init method is the same as in the book.

Tagging locations

  • Add the UITextFieldDelegate protocol to the list of delegates in WhereamiViewController.h.
    • Notice that there’s nothing else in the class declaration.
  • Add this method to WhereamiViewController.m. It is the same as in the book.

Putting the pieces together

  • We no longer have to declare private, internal-only methods.
  • Here’s the updated findLocation. We’re using properties and dot notation.
  • The updated foundLocation: method.
  • Finally, here’s the finished and updated locationManager:didUpdateLocations: method. This one replaced the deprecated method used in the book.
  • Deploy and run the app. Pretty cool, huh?