« previous: How to Protect Yourself from Viruses and Spyware | next: Count IP Addresses in Access Log File: BASH One-Liner »
Workaround for the "Could not find Library" Problem with the iPhone Remote App

The Remote app for the iPhone is an amazing thing: it turns your iPhone into a remote control for iTunes on your PC -- which is to say, a remote control for your stereo. But unlike the infrared remote that came with your stereo, this remote works over wifi, so while you’re out on the porch grilling up some steaks, you can take your iPhone out of your pocket and use it to play some music on the stereo inside.
But I’m having a serious problem with Remote that I can’t find much information about online: Remote connects to iTunes and works fine for a while, but then it suddenly fails to connect to iTunes, and cannot connect again until you go to the computer and close & re-open iTunes. My computer is a Mac mini PPC G4, but from posts in the Apple forums I know that some Windows users are having this problem too.
I had been running OS X Tiger 10.4.11; I upgraded to Leopard -- first 10.5.1 then 10.5.4 -- but that didn’t fix the problem. I rebooted the iPhone. I uninstalled the Remote app and reinstalled it. I tried another iPhone. I turned off the OS X firewall. I made sure OS X wasn’t going to sleep. I upgraded to iPhone OS 2.0.1. None of those things fixed the problem.
But I discovered that if I disabled & re-enabled my Mac’s network connection, that did fix the problem. So I immediately set to work creating a way to make this happen from the iPhone, since having to actually go over to the computer defeats the purpose of having a remote. The result is a 2-tap solution that lets you reset your Mac’s network connection from your iPhone. Here’s how.
Note: in the following instructions, replace "username" with your own username.
Step 1 of 6: Start Apache & Enable Your Sites Folder
In your Mac’s system prefs, go to Sharing, then enable Web Sharing. Then run this command:
sudo cp /etc/httpd/users/username.conf /etc/apache2/users/
Go into your /Users/username/Sites/ folder and create a new subfolder called "cgi-bin". Then edit the file /etc/apache2/users/username.conf and add the following line to the end of it:
ScriptAlias /~username/cgi-bin/ "/Users/username/Sites/cgi-bin/"
Now disable & re-enable Web Sharing, or run the command "sudo apachectl restart" which does the same thing.
Step 2 of 6: Create & Name Your Network Profiles
In your Mac’s system prefs, go to Network, then name your default network location "Online", and then create a second location called "Offline" which has all the adapters disabled.
And if your Mac doesn’t already have a fixed/static IP address, then give it one (in your "Online" location). If your automatic/dynamic IP was something like 192.168.1.100, then set its fixed IP to 192.168.1.200.
Step 3 of 6: Create CGI Script
Create the file /Users/username/Sites/cgi-bin/fix_itunes_remote.cgi and put the following into it:
#!/usr/bin/perl exit unless $ENV{REMOTE_ADDR} =~ /^192\.168\./; my $sentinel = "/Users/username/Public/Drop Box/fix_itunes_remote"; `touch "$sentinel"`; `chmod a+rw "$sentinel"`; print "Content-type: text/html\n\n"; print qq`<html>`; print qq`<body onload="javascript:window.opener='x';window.close();">`; print qq`</body></html>\n`;
Then run this command to make that file executable:
chmod a+x /Users/username/Sites/cgi-bin/fix_itunes_remote.cgi
Step 4 of 6: Create Shell Script
Create the file /Users/username/togglenet.sh and put the following into it:
#!/bin/bash SENTINEL="/Users/username/Public/Drop Box/fix_itunes_remote" for ((i=1; i<=9; i++)) do if [ -e "$SENTINEL" ] then echo "sentinel exists; fixing itunes remote now." rm -rf "$SENTINEL" /usr/sbin/scselect Offline sleep 5 /usr/sbin/scselect Online else sleep 5 fi done
Then run this command to make that file executable:
chmod a+x /Users/username/togglenet.sh
Step 5 of 6: Schedule Shell Script To Run Regularly
Run the command "crontab -e" and put the following into it:
*/1 * * * * /Users/username/togglenet.sh
Step 6 of 6: Use Your iPhone to Run the Script
On your iPhone, open Safari and visit this URL to reset your Mac’s network connection:
http://192.168.1.200/~username/cgi-bin/fix_itunes_remote.cgi
...where 192.168.1.200 is the IP address of your Mac. Note that the page will automatically close itself.
Create a bookmark for this page on your iPhone’s home screen, right next to your existing Remote app, and name the bookmark FixRemote. That way you can just tap FixRemote and then Remote and you’re in business.
That’s It
Visiting that URL causes the CGI script to create the sentinel file; when the shell script sees the sentinel file, it resets your Mac’s network connection.
The cronjob runs the shell script once per minute, all day every day. And the shell script sleeps for a few seconds then loops and runs again, so the frequency of checking is actually every few seconds, not just once per minute. If it finds the sentinel file, it turns off the network and then turns it back on again, by using the scselect command to temporarily switch your network to its "Offline" profile.