Geeky Stuff that isn't my novels

Coldfusion FindMyiPhone Component

Yet another requirement of my current project is to be able to periodically report back the physical location of the device as the drivers deliver their loads wherever they are deliverying them. Also handy to be able to detect if the device is charging and the battery status — and send an alert to the user if battery condition is a problem.

At first I looked at doing all of this within the browser, but the problem is that the driver’s will have to allow location status during during every login session. No, thanks. First, there will be problems when they accidentally hit “no.” Second, there will be problems when they intentionally hit “no.”

So I did some googling into ways to locate an iPad or iPhone remotely (since we’re standardizing this project on iPad’s). Surely there must be some app that can find an iphone. Maybe … find … my … iPhone.

Right. I’ve used that before, when I left my phone in a cab in Chicago last summer, as a matter of fact. A little bit of Googling later and I discovered PHP-FindMyiPhone by Alan Beebe (https://github.com/albeebe/PHP-FindMyiPhone/blob/master/class.findmyiphone.php). The PHP script appeared to do everything I needed it to and then some, but of course I’m not working on PHP servers, I’m working on Coldfusion servers. So my next step was to take Alan’s source code (which tested as working when I ran it with PHP on my Macbook) and rewrite it as a Coldfusion component.

My task here was to adapt the code and see if I could make it work. So I set up a CFC, then copied and pasted the PHP code into a comment. Then I began to work out the logic to make it work.

In Alan Beebe’s class, the first function is to initialize the class and authenticate. The PHP code looks like this:

PHP initialization

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

private$client=array(

"app-version"=>"4.0",

"user-agent"=>"FindMyiPhone/472.1 CFNetwork/711.1.12 Darwin/14.0.0",

"headers"=>array(

"X-Apple-Realm-Support"=>"1.0",

"X-Apple-Find-API-Ver"=>"3.0",

"X-Apple-AuthScheme"=>"UserIdGuest"

)

);

private$debug;

private$username;

private$password;

private$Apple_MMe_Host;

private$Apple_MMe_Scope;

public$devices=array();

/**

* This is where you initialize FindMyiPhone with your iCloud credentials

I basically had to iterate through to get this working. First I did the query, got the CFHTTP response, then started assogmomg values. At the end of the authenticate method, we return an iCloudObj to the init function, and that’s now available throughout the component.

The calling Coldfusion page only needs to pass in the username and password like this:

<cfset iCloudObject.init("email@domain.net","password")>

But we don’t want to just initialize it. We want to get the devices. So the next line calls the getDevices method:
<cfset deviceList=iCloudObject.getDevices()>

This is actually going to be two functions — first, getDevices() which will make the http request, then generateDevice() which returns a query containing the objects. Not that the logic here is a little different than the PHP class, but it meets my needs for this function. Here’s the code:

Now we’re getting somewhere. Back on my text page, I cfdumped the returned object from getDevices and here is what I got:

After multiple tries, it looks like I don’t get results 100% of the time — maybe 80 to 90. So some error correction is in order. But all in all, I was pleased to have a working findmyiphone function from Coldfusion.