This article is more than 1 year old

How I wrote an iPhone application

An introduction to the world of mobile Web 2.0

But we do have a working application which will also function on the Nokia implementation of Safari, but looks very odd on the desktop version.

Mainly we discovered that JavaScript is really irritating to program in, and that the idea of write-once-run-anywhere remains as true as it ever was. We also got annoyed at the security restrictions and the lack of comprehensive documentation, though at least the latter problem should fade with time.

We also decided that we're not going to develop anything else for the iPhone until there's a proper development kit, allowing the use of a proper programming language, and some decent documentation too. ®

<script language="JavaScript">
//<!-- JavaScript here
  
  //This one thinks it's an object
  var myRequest = new XMLHttpRequest();
  
  //This is the text we're going to change the word "iPhone" to
  var changeTo = "";
  
  //This is our home page, and the site that leaving will unload the app
  var home = 'http://www.theregister.com';
  
  function startUp() {
    changeTo = readCookie("newName")
    if (changeTo == null) {
      changeTo = window.prompt("So what would better suit the iPhone?");
      createCookie("newName", changeTo, 1);
    }
    loadRegister(home);
  }
  
  function loadRegister(targetURL) {
  
    //targetDomain is set to a string containing the site (but not directories or file) that the user clicked on
    var targetDomain = targetURL.substring(targetURL.indexOf(".", 8)+1, targetURL.indexOf("/", 8));

    //We compare that to our home page
    if (home.indexOf(targetDomain) == -1) {
      alert("Moving Off Site: " + targetDomain);
      //This line unloads this application, as the targetURL replaces this document
      parent.parent.location=targetURL;
    }

    //Then we load the page
    myRequest.open("GET", targetURL);
    myRequest.onload = targetLoaded;
    myRequest.send();
  }
  
  function targetLoaded() {
    var loadedSite = myRequest.responseText;
    
    loadedSite = loadedSite.replace(/iPhone /g, changeTo + " ");
    loadedSite = loadedSite.replace(/ iPhone/g, " " + changeTo);

    var counter;

    var loadedDocument = parent.frames[0].document;
    
    loadedDocument.open();
    loadedDocument.write(loadedSite);
    loadedDocument.close();
    //This is our horrible bodge which waits 10 seconds for the page to load
    setTimeout('pageLoaded()', 10000);
  }
  
  function pageLoaded() {
    //This loops through every link on the page (241 on the El Reg home page when we were testing this) and adds an "onclick" even listener
    for (i=0; i < parent.frames[0].document.links.length; i++) {
      parent.frames[0].document.links[i].onclick = linkClicked;
    }
  }
  
  function linkClicked() {
    loadRegister(this.href);
    //We return "false" so the browser dosen't attempt to load the link clicked on.
    return false;
  }
  
  function returnHome() {
    loadRegister(home);
  }
  
  function changeName() {
    eraseCookie("newName");
    changeTo = window.prompt("So what would better suit the iPhone?");
    createCookie("newName", changeTo, 1);
  }
  
  function createCookie(name,value,days) {
        if (days) {
                var date = new Date();
                date.setTime(date.getTime()+(days*24*60*60*1000));
                var expires = "; expires="+date.toGMTString();
        }
        else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
  }
  function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
                var c = ca[i];
                while (c.charAt(0)==' ') c = c.substring(1,c.length);
                if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
  }
  function eraseCookie(name) {
        createCookie(name,"",-1);
  }
//-->
</script>

More about

More about

More about

TIP US OFF

Send us news


Other stories you might like