Feeds

How I wrote an iPhone application

An introduction to the world of mobile Web 2.0

Eight steps to building an HP BladeSystem

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.co.uk';
  
  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>

Securing Web Applications Made Simple and Scalable

More from The Register

next story
Auntie remains MYSTIFIED by that weekend BBC iPlayer and website outage
Still doing 'forensics' on the caching layer – Beeb digi wonk
Apple orders huge MOUNTAIN of 80 MILLION 'Air' iPhone 6s
Bigger, harder trouser bulges foretold for fanbois
GoTenna: How does this 'magic' work?
An ideal product if you believe the Earth is flat
Telstra to KILL 2G network by end of 2016
GSM now stands for Grave-Seeking-Mobile network
Seeking LTE expert to insert small cells into BT customers' places
Is this the first step to a FON-a-like 4G network?
Yorkshire cops fail to grasp principle behind BT Fon Wi-Fi network
'Prevent people that are passing by to hook up to your network', pleads plod
BlackBerry: Toss the server, mate... BES is in the CLOUD now
BlackBerry Enterprise Services takes aim at SMEs - but there's a catch
prev story

Whitepapers

Designing a Defense for Mobile Applications
Learn about the various considerations for defending mobile applications - from the application architecture itself to the myriad testing technologies.
Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Top 8 considerations to enable and simplify mobility
In this whitepaper learn how to successfully add mobile capabilities simply and cost effectively.
Seven Steps to Software Security
Seven practical steps you can begin to take today to secure your applications and prevent the damages a successful cyber-attack can cause.
Boost IT visibility and business value
How building a great service catalog relieves pressure points and demonstrates the value of IT service management.