Implementing a basic location service on XCode 5 for IOS7
This is based on the book: iOS Programming: The Big Nerd Ranch Guide (3rd Edition) (Big Nerd Ranch Guides). I recommend this book because
it receives great recommendation from Amazon
i am using it myself and it really helps me to understand about objective c, cocoa framework, object-oriented and design patterns. it explains about the class objects from cocoa framework and how each classes are related with examples (e.g: inheritance, collection). it also explains about different design patterns such as delegation (callback in objective c), mvc, chain reaction and probably much more (ie: i am still reading it)
Here is my example of creating a simple location services on XCode5 with ios7
Figure 1: Screenshot of XCode which includes step 1 to 3 on creating a new Whereami project
On XCode, go to File > New > Project. Create a new Single View Application call "whereami" for iPhone. Important: Please note that creating a Single View Application in Xcode5 includes STORYBOARD. When you're using STORYBOARD, be sure to use initWithCoder. If you're NOT using STORYBOARD, use initWithNibName
Add CoreLocation.framework. Refer to Figure 1.
On XCode > Project Navigator, click on the top most "Whereami" (Step 1, Figure 1).
On the Editor area, Click go to whereami targets (Step 2, Figure 1)
Expand Link Binary With Libraries (Step 3, Figure 1)
You should see 3 items: CoreGraphics.framework, UIKit.framework, Foundation.framework. Click on the + sign to add CoreLocation.framework
In WhereamiViewController.h do the following:
Import CoreLocation framework. Underneath import <UIKit/UIKit.h> add the following code:
#import <CoreLocation/CoreLocation.h>
Declare that WhereamiViewer conforms to the CLLocationManagerDelegate protocol by adding the the protocols within angled brackets in the interface declaration following the superclass like this:
@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>
Create CLLocationManager instance
CLLocationManager *locationManager;
The final code should look like this:
#import <UIKit/UIKit.h> #import <CoreLocation/CoreLocation.h> @interface WhereamiViewController : UIViewController <CLLocationManagerDelegate> { CLLocationManager *locationManager; } @end
In WhereamiViewController.m, add the following codes:
#import "WhereamiViewController.h" @implementation WhereamiViewController -(id)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:(NSCoder *)aDecoder]; if (self) { locationManager = [[CLLocationManager alloc] init]; [locationManager setDelegate:self]; [locationManager setDesiredAccuracy:kCLLocationAccuracyBest]; [locationManager startUpdatingLocation]; } return self; } -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { NSLog(@"%@",[locations lastObject]); } -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { NSLog(@"Could not find location:%@", error); } -(void)dealloc { // Tell the location manager to stop sending us messages [locationManager setDelegate:nil]; } @end
Build and run the program. When you're running the simulator, you'll have to simulate its location by clicking the
icon, and select any location from the drop-down list.
If you're running for the first time, you will need to give permission for the application to use location services on the device. Wait for a few seconds while the location is found and you will see results in your console like this:
2014-01-25 08:32:48.905 Whereami[1355:70b] +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/25/14, 8:32:48 AM Singapore Standard Time 2014-01-25 08:32:49.668 Whereami[1355:70b] +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/25/14, 8:32:49 AM Singapore Standard Time 2014-01-25 08:32:50.669 Whereami[1355:70b] +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/25/14, 8:32:50 AM Singapore Standard Time 2014-01-25 08:32:51.670 Whereami[1355:70b] +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/25/14, 8:32:51 AM Singapore Standard Time 2014-01-25 08:32:52.671 Whereami[1355:70b] +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/25/14, 8:32:52 AM Singapore Standard Time 2014-01-25 08:32:53.673 Whereami[1355:70b] +/- 5.00m (speed -1.00 mps / course -1.00) @ 1/25/14, 8:32:53 AM Singapore Standard Time Program ended with exit code: 0
IOS Programming, The Big Nerd Ranch Guide edition 3
I refer here to fix certain codes for it to be compatible with IOS7, xcode 5