ref:
http://chrisalvares.com/blog/7/creating-an-iphone-daemon-part-1/
http://chrisalvares.com/blog/30/creating-an-iphone-daemon-part-2/
http://chrisalvares.com/blog/35/creating-an-iphone-daemon-part-3/
http://chrisalvares.com/blog/38/creating-an-iphone-daemon-part-4/
http://chrisalvares.com/blog/72/creating-an-iphone-daemon-part-5/
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.
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.
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
So lets go through this line by line starting with our imports
The most important import we have here is the CoreLocation framework, this will let us use the CLLocationManager class.
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
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).
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 .
We will use these functions to start the GPS after a certain amount of time.
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)
This is part three of the blog series “Creating an iPhone Daemon”, for part one please click this link
In the last part of the series, we created the header file for our DLocationDelegate class, now lets create the implementation file (DLocationDelegate.h)
Yes, it is a pretty big file, so I won’t explain it all (it has comments to help you).
These two lines are important, after we init the locationManager we must make sure that the delegate is set to our DLocationDelegate class.
Inside this function, you will notice the CLLocation *newlocation’s horizontalAccuracy property. If the horizontal accuracy is less than 0, than there was an error, if it is greater than 100, the accuracy is very poor, so the DLocationDelegate class will wait for a better reading.
This line is VERY important, we have to stop the GPS from updating, otherwise the iPhone’s batter will die super quickly.
When you put everything together, you should get a link like:
http://youriphonelocationserver.com/locationService.php?ID=iphoneid&LATITUDE=laditudeNumber&LONGITUDE=longitudeNumber&ALTITUDE=number&RANDOM=12312
We will create this PHP file in an upcoming tutorial.
The DLocationDelegate timer is set for 1800.0 seconds, which is 30 minutes. I found that 30 minutes does not drain the battery too much, but still has a good amount of readings just incase your iPhone is lost.
While this will get your DLocationDelegate class setup, we still have one more issue, and that is that the iPhone will go into a hard sleep after a minute of no use. We will fix this in an upcoming tutorial.
This is part four of the blog series “Creating an iPhone Daemon”, for part three please click this link
This is the coolest part of the series. Up until now, all the code can only be used as a standalone application that can only run if the user clicks the “DLocationDemon.app” button on the springboard. Now it is time to put it all together, with the Daemon Plist file.
You will also need SSH installed for this part (again, the hackint0sh forums) I use Cyberduck as my SSH client (Select SFTP for SSH and type in the IP of your iPhone with root/alpine as username/password respectively).
The first thing we do is compile our DLocationDemon project. Please use the method over at hackint0sh to compile and codesign your project successfully.
Once you have compiled your project, you should get a DLocationDemon.app in the “Products folder” right click on the .app file and click “Reveal in Finder”.
Right click on the .app file and click “Show Package Contents” and make sure you have a DLocationDemon file inside the folder
Next, using your favorite SFTP program, dumb the DLocationDemon.app inside the /Applications folder of your iPhone
**NOTE** Make sure this is the /Applications folder and not the /var/root/Applications folder. You can tell the difference because all of the apps you downloaded from the AppStore should be in the /Applications folder.
The next thing we have to do is create the .plist file for the daemon to work.
I have named mine com.chrisalvares. DLocationDemon.plist, this should match the bundle identifier in the info.plist file.
Make sure the label field matches the bundle identifier in your info.plist file.
Now SFTP into your iPhone again and drop this plist file into the “/System/Library/LaunchDaemons” folder. You should see at least one other daemon.plist file there. (I believe this is where the SSH daemon is).
Now restart your iPhone.
This will give you a working daemon installed on your iPhone!
This is part five of the blog series “Creating an iPhone Daemon”, for part four please click this link
iPhone 3.0 just came out, and while the old code set will still work with it, I decided to go back and clean up and organize the code a little bit more. Here is the CALocationDemon Xcode project. I also equipped it with the PHP and daemon plist file needed.
Get it here (http://chrisalvares.com/blog/wp-content/uploads/2009/06/calocationdemon.zip)