Many Cocoa developers might want to port their application(s) to the iPhone. I’m one of these developers and the first problem I ran into was how to sync my desktop application’s data with it’s iPhone pendant. Some applications like OmniFocus use MobileMe or any WebDav server for synchronisation. Unfortunatly this is not an option for me because I don’t want my users to have a MobileMe account or a Server just to sync with my app.
My solution uses Bonjour to synchronize my app’s data to the iPhone via the network. You can download this demo project which contains a proof of concept version of my solution. The demo project consists of two XCode Projects. One desktop app and it’s iPhone pendant.
The idea is pretty simple: The desktop application needs to share it’s data with the iPhone app via the network. Therefore the iPhone app creates a sync service and publishes it using the Bonjour technology. This way, the desktop application “sees” all iPhones (which are running the sync service) in the local network and offers the user a list from which he can choose the iPhone to sync with.
The application’s data is then transfered onto the iPhone where it is saved as a file. The iPhone app can now work with this data.
As I’m short of time at the moment I could only implement this basic solution yet but I’m sure I will extend it as soon as I’ll find the time.
In a real world scenario the synchronization process might be a bit more complex. In most cases you may want to import your iPone app’s data into your desktop app and vice versa. For this I’d suggest to put the whole sync logic into your desktop app and perform something like this:
- Fetch the data from the iPhone
- Check it against your desktop app’s data
- Resolve conflicts if they exist
- Import the data into your desktop app
- Put the whole data back to the iPhone
Most Important: Security
Whenever you deal with network services you have to keep security in mind. When you start a network service on your iPhone it might be a potential security risk, since it opens a “door” to your applications data and everyone in the local network may be able to download this data.
So you may want to create some kind of pairing or authentication protocol as well as encryption for your sync service. This means that when the app syncs for the very first time the user must enter some “secret” which identifies the the iPhone whic the user wants to sync with. Of course this mechanism becomes useless when you use an unencrypted protocol so you should think about encryption too.
Besides, you shoud ensure that the sync service is only stated when it is really needed. E.g. stop the service when the user swithes to some view other than the sync view, etc.
Since the iPhone supports Bonjour, it could be used to sync data with any desktop application.
A full featured example project resides on your HD in /Developer/Examples/Foundation/PictureSharing and /Developer/Examples/Foundation/PictureSharingBrowser. This is where I took some code from :).
Since I’ve never held a real iPhone in my hands I only tried the example Project with the simulator. It would be nice to see if it works on real hardware. So, if you have tried it, please drop me a line.