`

Creating an iPhone Daemon – Part 2

    博客分类:
  • ios
 
阅读更多

Here is part two of the blog series “Creating an iPhone Daemon”, for part one please click this link

As I said in part 1, we will be creating the DLocationDelegate class.
With most daemons, you do a task like read a file, do something with that file, sleep for a certain amount of time, then check for file changes, and repeat the steps over again. Unfortunately, with GPS coordinates, we have to wait for the CoreLocation delegate to give us the coordinates. The thing about Objective-C and Apple’s Cocoa framework is that most of the classes depend heavily on delegates. This is also true when dealing with the CoreLocation APIs. So lets get coding.

So lets write the DLocationDelegate header file first, this will give us a good look at what is ahead

#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
 
@interface DLocationDelegate : NSObject <CLLocationManagerDelegate>
{
	BOOL trackingGPS;
	CLLocationManager *locationManager;
}
 
@property (nonatomic, retain) CLLocationManager *locationManager;
 
- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
		   fromLocation:(CLLocation *)oldLocation;
 
- (void)locationManager:(CLLocationManager *)manager
	   didFailWithError:(NSError *)error;
 
-(void) startIt:(NSTimer *) timer;
-(void) startItAgain:(NSTimer *)timer;
 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
 
@end

So lets go through this line by line starting with our imports

#import <CoreLocation/CoreLocation.h>
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>

The most important import we have here is the CoreLocation framework, this will let us use the CLLocationManager class.

@interface DLocationDelegate : NSObject <CLLocationManagerDelegate>

When you have a class name in <> symbols, Objective-C now knows that you are implementing methods from this class. In this case to receive the GPS coordinates we use the CLLocationManagerDelegate protocol

	BOOL trackingGPS;
	CLLocationManager *locationManager;

The Boolean trackingGPS will tell us if we are currently tracking the GPS, this will be used to tell if the CLLocationManager is currently looking for coordinates. The locationManager is the actual class that will get the GPS coordinates from either the GPS (iPhone 3G) or the cell towers (iPhone 2G).

- (void)locationManager:(CLLocationManager *)manager
	didUpdateToLocation:(CLLocation *)newLocation
		   fromLocation:(CLLocation *)oldLocation;
 
- (void)locationManager:(CLLocationManager *)manager
	   didFailWithError:(NSError *)error;

These are the delegate methods for the CLLocationManagerDelegate. The first one “didUpdateToLocation” will give us the coordinates using a CLLocation class which contains a longitude, latitude, and sometimes even a altitude. The second function will notify our DLocationDelegate of any errors with the GPS, maybe if your iPhone is inside a lead case :) .

-(void) startIt:(NSTimer *) timer;
-(void) startItAgain:(NSTimer *)timer;

We will use these functions to start the GPS after a certain amount of time.

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

This is for the NSURLConnection object that will send the GPS coordinates to our server.

In part three of this tutorial we will create the DLocationDelegate.m file (the implementation file)

-Chris

转自 :http://chrisalvares.com/blog/30/creating-an-iphone-daemon-part-2/

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics