iOS:	
  Frameworks,	
  Delega3on	
  and	
  MapKit	
  


                Jussi	
  Pohjolainen	
  
    Tampere	
  University	
  of	
  Applied	
  Sciences	
  
About	
  Frameworks	
  
•  Framework	
  is	
  a	
  
   collec3on	
  of	
  classes	
  
   that	
  are	
  added	
  to	
  target	
  
•  Target	
  =	
  the	
  compiled	
  
   iOS	
  app	
  
•  Cocoa	
  Touch	
  has	
  
   several	
  frameworks	
  
•  You	
  can	
  add	
  and	
  
   remove	
  frameworks	
  in	
  
   your	
  project	
  
NSString	
  




NSString	
  needs	
  the	
  Founda3on	
  framework!	
  
Add	
  a	
  Framework	
  




Choose	
  Target	
  
Select	
  Build	
  Phases	
  
Click	
  +	
  (Link	
  Binary	
  with	
  Libraries)	
  
About	
  Delegate	
  
// Create location manager
LocationManager* lc = [[LocationManager
alloc] init];

// Who is receiving location updates? It’s
// someObject. What is someObject? It’s
// whatever object that implements a
// certain protocol!
[lc setDelegate: someObject];
Documenta3on	
  




          Whatever	
  object,	
  
            but	
  is	
  has	
  to	
  
             implement	
  
         CLoca3oinManager
         Delegate	
  Protocol!	
  
About	
  Delegate	
  
// Create location manager
lc = [[LocationManager alloc] init];
// Set delegate this “this”-object
[lc setDelegate: self];
Implemen3ng	
  a	
  Delegate	
  
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MyLocationViewController :
UIViewController<CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;
}
Delegate	
  Methods	
  
Implementa3on	
  
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations
{
    // Implementation here
}
Delega3on	
  
•  Delega3on:	
  OO	
  way	
  of	
  making	
  callback	
  
•  Send	
  messages	
  to	
  some	
  object.	
  Some	
  object	
  must	
  have	
  
   methods	
  that	
  are	
  defined	
  in	
  the	
  protocol	
  
•  The	
  same	
  than	
  in	
  Java	
  
    –  buUon.addAc3onlistener(Ac3onListener	
  x)	
  
    –  Where	
  x	
  is	
  an	
  interface	
  
•  By	
  default,	
  protocol	
  methods	
  are	
  mandatory.	
  But	
  
   protocol	
  may	
  hold	
  op-onal	
  methods!	
  
•  Typically	
  in	
  delegate	
  protocols,	
  the	
  methods	
  are	
  
   op-onal	
  
Delega3on	
  
•  If	
  delega3on	
  protocol	
  method	
  is	
  mandatory,	
  
   it’s	
  called	
  without	
  checking	
  
•  If	
  delega3on	
  protocol	
  method	
  is	
  op3onal,	
  
   then	
  checking	
  is	
  done:	
  if	
  target	
  object	
  contains	
  
   this	
  method,	
  call	
  it.	
  
    –  This	
  happens	
  in	
  run3me,	
  you	
  don’t	
  have	
  to	
  worry	
  
       about	
  this.	
  
CLLoca3onManagerDelegate.h	
  

            All	
  methods	
  are	
  op3onal!	
  
Demo:	
  Using	
  Debugger	
  
MAPKIT	
  
MapKit	
  Framework	
  
•  Interface	
  for	
  embedding	
  maps	
  into	
  view	
  
•  Supports	
  also	
  
    –  Annota3ng	
  map	
  
    –  Adding	
  overlay	
  
    –  Reverse	
  geocoding	
  
•  As	
  of	
  iOS	
  6	
  Apple	
  uses	
  it’s	
  own	
  maps	
  instead	
  of	
  
   Google	
  maps	
  
•  Reference	
  
    –  hUp://developer.apple.com/library/ios/
       #documenta3on/MapKit/Reference/
       MapKit_Framework_Reference/_index.html	
  
How?	
  
•  Add	
  MapKit	
  framework	
  to	
  your	
  project	
  
•  Import	
  MapKit	
  –	
  class	
  
   –  #import <MapKit/MapKit.h>
•  Use	
  MKMapView	
  object	
  
   –  IBOutlet MKMapView *worldView;
•  Show	
  user	
  loca3on?	
  
   –  Set	
  showsUserLocation	
  property	
  to	
  YES
•  Use	
  MKMapViewDelegate	
  protocol
MKMapViewDelegate	
  


             This	
  is	
  called	
  when	
  user	
  loca3on	
  
              changes.	
  In	
  here	
  you	
  can	
  get	
  
              the	
  coordinates	
  and	
  zoom	
  in	
  
                                MapView	
  
Annota3ons	
  
•  Display	
  something	
  on	
  a	
  map:	
  annota3on	
  
•  Annota3on	
  is	
  any	
  object	
  that	
  conforms	
  to	
  
   MKAnnotation	
  protocol	
  
    –  These	
  objects	
  can	
  be	
  added	
  to	
  MKMapView	
  
•  MKAnnota3on	
  Reference	
  
    –  An	
  object	
  that	
  adopts	
  this	
  protocol	
  must	
  
       implement	
  the	
  coordinate	
  property.	
  The	
  other	
  
       methods	
  of	
  this	
  protocol	
  are	
  op3onal.	
  
MapPoint	
  
Add	
  MapPoint	
  to	
  Map	
  

iOS: Frameworks and Delegation

  • 1.
    iOS:  Frameworks,  Delega3on  and  MapKit   Jussi  Pohjolainen   Tampere  University  of  Applied  Sciences  
  • 2.
    About  Frameworks   • Framework  is  a   collec3on  of  classes   that  are  added  to  target   •  Target  =  the  compiled   iOS  app   •  Cocoa  Touch  has   several  frameworks   •  You  can  add  and   remove  frameworks  in   your  project  
  • 3.
    NSString   NSString  needs  the  Founda3on  framework!  
  • 4.
    Add  a  Framework   Choose  Target   Select  Build  Phases   Click  +  (Link  Binary  with  Libraries)  
  • 5.
    About  Delegate   //Create location manager LocationManager* lc = [[LocationManager alloc] init]; // Who is receiving location updates? It’s // someObject. What is someObject? It’s // whatever object that implements a // certain protocol! [lc setDelegate: someObject];
  • 6.
    Documenta3on   Whatever  object,   but  is  has  to   implement   CLoca3oinManager Delegate  Protocol!  
  • 7.
    About  Delegate   //Create location manager lc = [[LocationManager alloc] init]; // Set delegate this “this”-object [lc setDelegate: self];
  • 8.
    Implemen3ng  a  Delegate   #import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface MyLocationViewController : UIViewController<CLLocationManagerDelegate> { CLLocationManager *locationManager; }
  • 9.
  • 10.
    Implementa3on   - (void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray *)locations { // Implementation here }
  • 11.
    Delega3on   •  Delega3on:  OO  way  of  making  callback   •  Send  messages  to  some  object.  Some  object  must  have   methods  that  are  defined  in  the  protocol   •  The  same  than  in  Java   –  buUon.addAc3onlistener(Ac3onListener  x)   –  Where  x  is  an  interface   •  By  default,  protocol  methods  are  mandatory.  But   protocol  may  hold  op-onal  methods!   •  Typically  in  delegate  protocols,  the  methods  are   op-onal  
  • 12.
    Delega3on   •  If  delega3on  protocol  method  is  mandatory,   it’s  called  without  checking   •  If  delega3on  protocol  method  is  op3onal,   then  checking  is  done:  if  target  object  contains   this  method,  call  it.   –  This  happens  in  run3me,  you  don’t  have  to  worry   about  this.  
  • 13.
    CLLoca3onManagerDelegate.h   All  methods  are  op3onal!  
  • 14.
  • 15.
  • 16.
    MapKit  Framework   • Interface  for  embedding  maps  into  view   •  Supports  also   –  Annota3ng  map   –  Adding  overlay   –  Reverse  geocoding   •  As  of  iOS  6  Apple  uses  it’s  own  maps  instead  of   Google  maps   •  Reference   –  hUp://developer.apple.com/library/ios/ #documenta3on/MapKit/Reference/ MapKit_Framework_Reference/_index.html  
  • 17.
    How?   •  Add  MapKit  framework  to  your  project   •  Import  MapKit  –  class   –  #import <MapKit/MapKit.h> •  Use  MKMapView  object   –  IBOutlet MKMapView *worldView; •  Show  user  loca3on?   –  Set  showsUserLocation  property  to  YES •  Use  MKMapViewDelegate  protocol
  • 18.
    MKMapViewDelegate   This  is  called  when  user  loca3on   changes.  In  here  you  can  get   the  coordinates  and  zoom  in   MapView  
  • 19.
    Annota3ons   •  Display  something  on  a  map:  annota3on   •  Annota3on  is  any  object  that  conforms  to   MKAnnotation  protocol   –  These  objects  can  be  added  to  MKMapView   •  MKAnnota3on  Reference   –  An  object  that  adopts  this  protocol  must   implement  the  coordinate  property.  The  other   methods  of  this  protocol  are  op3onal.  
  • 20.
  • 21.