Creating an iPhone Daemon – Part 1

So I thought I would start this blog of with a BANG!, and show people how to create an iPhone Daemon.

daemon is a program or application that runs in the background on the device. This differs from a regular program as it is started on boot of the device, and can run at certain intervals without the users permission. I am going to show you how to create a very simple one that polls the GPS on the iPhone, and stores the information on a database.

You will need to do the following things before we can get started.

1) Make sure your iPhone is jail broken
2) Install openSSH on your iPhone via Cyndia
3) xcode has the tool chain installed, if it does not there are very easy instructions over at thehackint0sh forums
4) Install Cyberduck/or another SFTP program on your mac

So now that we have done this, lets get coding!

Open up xcode and start a new xcode project with the open tool chain template.

The first thing to note is that it is not good to use the UIApplication class to start your daemon (it takes more memory than we need), so we are going to write our own main method.

#import
#import "CALocationDelegate.h"
 
int main(int argc, char *argv[]) {
//start a pool
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
//initialize our LocationManager delegate so we can pick up GPS information
DLocationDelegate *obj = [[DLocationDelegate alloc] init];
 
//start a timer so that the process does not exit, this will GPS time to fetch and come back.
NSDate *now = [[NSDate alloc] init];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:now
interval:.01
target:obj
selector:@selector(startIt:)
userInfo:nil
repeats:YES];
 
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[runLoop run];
 
[pool release];
NSLog(@"Finished Everything, now closing");
return 0;
}

You might notice that we started a timer and used the NSRunloop-run method. The run method blocks the thread, but still lets delegates (like the CoreLocationDelegate) receive delegate methods.

On the next segment, we will create the DLocationDelegate class to receive GPS coordinates, and send them to a server.

转自 http://chrisalvares.com/blog/page/6/

你可能感兴趣的:(iPhone)