Find My iPhone via Node.js
The Find My iPhone app is a great feature of iCloud. You can track your iCloud enabled devices to determine if you left your iPad at work or your iPhone on the bus; you can send messages or play sounds to help locate and recover them; and you can remotely lock or wipe them if you are concerned about the device being stolen. There are limitations to this service however: you must use the official Apple app (or iCloud web-page) and you cannot keep a history of previous locations.
I thought there must be a way to tap into the service to use the information for my own purposes. After-all, behind most internet-enabled things these days there is a simple JSON API. A quick Google search revealed a few people had the same idea and led me to find a few GitHub repositories for APIs that wrapped around the Find My iPhone web service.
Note: I have no idea if the iCloud EULA allows for this kind of thing. I’m not being malicious and I’m not hammering their web-service, so I think it’s probably okay. Additionally, Apple could again change their API at any time and break this implementation. Use at your own risk!
Susomi – PHP Script
I initially trialled Susomi, a PHP script that allowed full-access to the Find My iPhone service, including locating devices, sending messages, remote locking and wiping of lost devices. A year ago this script worked great. Unfortunately since that time, it seems that the Find My iPhone API has changed and the script has not been updated to keep up with the changes.
I did find a gist that promised to support the updated API and a few other repositories that supposedly worked, but I had little success. Additionally, I wanted to experiment with something a bit more “cutting edge” than PHP so rather than repairing the scripts myself, I started looking for Node.js equivalents.
iPhone-Finder – Node.js
Further searches of GitHub revealed a couple of Node.js libraries for interacting with the Find My iPhone API. Similar to my PHP search, a lot of these packages had been written once and never updated when the API changed. Fortunately, I discovered iphone-finder and it seemed to work out of the box.
The features supported by the lightweight iphone-finder package are much more limited than Susomi: you can only retrieve the “device info”, rather than having the option to send messages, lock the device, etc. However, for the purposes I planned on using this API, this was perfect.
Getting Your iPhone’s Location
First you need to install the iphone-finder library. A simple NPM command will sort that out:
npm install iphone-finder
You also need to provide iphone-finder with your iCloud username and password. As usual, you should take precautions to make sure any script is not trying to steal your personal information. You can browse the iphone-finder code on GitHub to reassure yourself it isn’t doing anything bad.
The following Node.js snippet will print the name, type and location of the newest device on your iCloud profile to the console.
var iPhoneFinder = require('iphone-finder'); var iCloudUser = '[email protected]'; var iCloudPass = '#############'; iPhoneFinder.findAllDevices(iCloudUser, iCloudPass, function(err, devices) { var device = devices[0]; console.log("Device name: " + device.name); console.log("Device Type: " + device.modelDisplayName); console.log("Device Latitude: " + device.location.latitude); console.log("Device Longitude: " + device.location.longitude); });
Executing the above will result in the something similar to the following output:
Device name: Chris' iPhone Device Type: iPhone Device Latitude: 55.948559 Device Longitude: -3.199771
The device variable contains lots of information, including: the horizontal accuracy of the location (in metres); the type of location fix (GPS / Wifi / etc); the battery level; the time-stamp of the location fix; and details regarding the device being lost, etc. Full details are available on this gist.
Building on Basic Tracking
The above code snippet provides a useful base to build on. Knowing the current location of an iCloud connected device is useful and the API enables this basic information to be used to build a larger application or utility.
What are the possibilities?
- I have written a Node.js application to poll the location of my iPhone at regular intervals and log the location. This application provides a historical trace of where I’ve been and shows the places I frequent most. You can read more about it here.
- I have also integrated the iCloud device location with my magic screen project to provide a high-level estimate of my current location. More details are coming soon.
- It would be possible to integrate the phone’s location with a set of rules to perform tasks based on the location. For example, if you were travelling between work and home the phone could turn on the heating in your house.
- A web service could send you messages with information from Wikipedia about the nearest place of interest as you walked around a city.