Chris Alvares' Blog

Creating an iPhone Daemon – Part 1

Author :admin     Category : Chris Alvares, Programming     24 Feb | 18 Comments

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

A 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 the hackint0sh 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.

Tags: , , , , ,

16 Comments

  1. ArianaRalf
    Posted May 13, 2009 at 11:31 pm | Permalink

    I am very interested in this

  2. sup frog!
    Posted June 5, 2009 at 1:20 pm | Permalink

    is that first line supposed to be

    #import {CoreLocation/CoreLocation.h}

    where the { } are HTML carot brackets? Maybe they got censored in the blog post?

  3. Posted July 19, 2009 at 11:39 pm | Permalink

    Thank you for a great blog, I will be sure to bookmark your site and check later… Usually I don’t leave a comment but I wanted to let you know that I really like your site :)

  4. Posted September 9, 2009 at 8:56 pm | Permalink

    Usually I don’t leave a comment but I wanted to let you know that I really like your site.

  5. amu
    Posted December 16, 2009 at 11:50 am | Permalink

    Hi!
    any one can helpmeout..while i am using this application ..when i placed calocation daemon app build in coreservices..to run as background daemon..
    iphone gives me a pop-up alert…calocationdaemon app uses your current location..i want to hide this pop-up alert ..dont want to show this pop-up alert to user..how it can be possible through code .

    thanks

  6. raziiq
    Posted January 4, 2010 at 10:50 pm | Permalink

    Hi there, thanks for that nice tutorial.

    I have one question, do i need to add this whole code in a Navigation Based App Template?

  7. Posted June 30, 2010 at 5:29 am | Permalink

    Thanks for the info! I have a question though: can I create a daemon to open an app right after the boot process completes?

  8. Francine Lacoste
    Posted December 9, 2010 at 6:47 am | Permalink

    Please help me someone just still my I-Phone (nov 11, 2010)
    I have the serial number and I like to register my i-phone because there is many personal information in it.
    How can I register my i-phone, please help me?

  9. Posted May 4, 2011 at 2:40 pm | Permalink

    Nice one. “iPhone is jail broken” I’ve got a few codes.

  10. Posted May 5, 2011 at 8:42 am | Permalink

    Willingly I accept. In my opinion, it is actual, I will take part in discussion. I know, that together we can come to a right answer.
    [url=http://rozwiazywaniezadan.com]Rozwiazywanie zadan [/url]

  11. Posted May 15, 2011 at 7:54 am | Permalink

    thanks i’ve searching for this :)

  12. Posted August 10, 2011 at 1:13 pm | Permalink

    you very first ought to call TN to see if they even will accept your license. i know where im at you’ve to do some far more points if you are moving from out of state to mine. like take one more test etc…. so call and ask them what you need to do. http://weifjdsflkdjs.com

  13. abort
    Posted March 8, 2012 at 2:40 am | Permalink

    Does anyone know how to do that by now? There don’t seem to be templates out for XCode 4.2

  14. Jeka
    Posted May 10, 2012 at 6:38 am | Permalink

    What about xcode 4.3.2???

  15. Posted July 11, 2012 at 2:41 am | Permalink

    Does anyone know how to do that by now? There don’t seem to be templates out for XCode 4.2

  16. Dennis Krols
    Posted July 29, 2012 at 4:25 am | Permalink

    Hi thanks for this article!
    but how can i check if the deamon is running?
    and are those runned as root?

2 Trackbacks

  1. By Writing a daemon? | TechyPenguin on March 25, 2009 at 1:42 am

    [...] a file, search it for a string, replace the string with another, and close it). I found this site: Chris Alvares blog Β» Creating an iPhone Daemon – Part 1, but he didn’t post any other [...]

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

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Latest News

11 Jun

Summer Projects

After watching Apple’s WWDC keynote speech, I became motivated to write a blog post detailing my projects for the Summer of 2012. Unlike the previous summers, which focused on

30 Jan

How to: Coda in Fullscreen

So I finally started doing some web development on my new iMac running OSX Lion, and I was a little shocked to see that Coda had not implemented the

About Me

About me Let me tell you a day in the life of Chris Alvares. I work at a marketing agency, creating internal projects to make the company run more efficiently. I come home and unwind, eat dinner, and play a couple of