Tag Archives: iphone

My delicious.com bookmarks for February 8th through February 9th

  • The rise and rise of the cognitive elite – "It seems unfair that footballers, bankers and tycoons earn more money than they know what to do with whereas jobless folk and single parents struggle to pay the rent, notes Mr Saunders. Yet it also seems unfair to take money from those who have worked hard and give it to those who have not, or to take away the profits of those who have risked their life savings to bring a new invention to market in order to help those who have risked nothing."
  • Jaguar E-Type turns 50 – Some designs can stand the test of time. The Jaguar E-Type is one of them.
  • iOS Debugging Magic – Some great iOS debugging hints.

My delicious.com bookmarks for January 23rd through January 29th

Programming Is Hard

It’s hard to explain to someone who is not already a programmer the kinds of things that you have to do when building an application. The thing you’re trying to explain is often very abstract and the answer frequently would involve lots of code.

That’s why I thought this particular problem might make an interesting discussion. In talking about this very simple problem we can talk about the things that developers deal with every day and, hopefully, the process can be followed by most people who have used an iPhone (or, actually, any computer). You won’t be a programmer at the end but you might have a greater appreciation for what happens behind the scenes.

The basic problem can be seen in this screen shot.

The login button is a toggle. That is, if I’m currently logged in it says “Log out.” And when I’m not logged in it says “Connect,” which is “log in” in Facebook parlance. A subtlety that you might not ordinarily notice is that when the user clicks the button, the keyboard should move out of the way when it’s not needed and come back again when it is.

There are two basic cases to consider:

  • When I’m not already logged in. In this case pressing the button opens the login screen and the keyboard should shift out of the way.
  • When I am logged in, pressing the button just logs me straight out. In this case the keyboard can stay where it is.

Easy, eh? (That’s what I thought when I started, too.)

Let’s sketch out the code.

when the button is pressed
  if I'm not logged in then
    make the keyboard move out of the way
    log in
  else
    log out

This is called pseudo code because while a computer can’t actually execute it we’ve broken down the processing into the required basic steps. You’ll have to trust me when I say that the real code (called Objective-C) isn’t terribly different.

Not so hard.

Except… you didn’t really think it would be that easy, did you?

It turns out we’re missing one important detail from our algorithm.

One thing we do a lot when programming is use pre-existing components (standing on the shoulders of giants as Isaac Newton, or Oasis, would have it). When you write a component you’re going to come at the problem with some specific ideas of how it’s likely to be used. Hopefully these ideas are the most common ones as this will make everyone else’s life significantly easier.

In this case, our button — a pre-existing component that I didn’t write — is a clever one. It already “knows” how to log in and log out of the service. This sounds like a great time saver and it simplifies our algorithm to just this:

when the button is pressed
  if I'm not logged in then
    make the keyboard move out of the way
  else
    do nothing

Only, it turns out that that doesn’t work.

Why not? Well, the implicit assumption in the above pseudo-code is that it is run first, that is, before the instruction to log us into or out of the service. But if the button logs us out before any of my code is run then no matter whether I was logged in or not before I pressed the button, the above code will find that we’re logged out and move the keyboard out of the way.

When you’re writing a user interface you find dozens of these tiny little details getting in the way of what you’re trying to do. Any non-trivial screen will have lots of these little problems, and the worst thing? No-one will ever know. If you do your job well no user will ever see these little glitches.

Talking of which, how do we eliminate the anomaly that we’ve just found? The solution is pretty simple. At the moment we’re trusting that the button “knows” whether we’re logged in or not. Instead, we’re going to have to take responsibility for doing that as well. That adds a little bit of complexity but isn’t too scary:

  • When the screen opens we need to check whether we’re logged in or not and remember that. In Objective-C we call that a “variable,” but really that just means something that we’re going to store and access again later
  • Later, when the button is pressed we can use the following pseudo-code:

    if the value I saved says that I'm not logged in then
      make the keyboard move out of the way
    else
      change the stored value to "not logged in"
    

  • When I’ve finished logging in — remember I could press the cancel button in the log in screen — change the stored value to “logged in”

And that’s it, we’re done. We have a working version of the log in button.

The final point I’m going to make here is that I ended up discarding all the code above. It just didn’t work as well as I wanted and I ended up implementing it in a completely different way. And this really is the kind of detail that you never notice. A good chuck of any programming project is prototyping, building mock-ups and other activities that don’t directly end up in any shipping product. All this discarded work — which does, at times feel like a waste of time — is designed to make the product better.

Some people who have never programmed say things like “there’s nothing complicated there” or “it’s only a button!” Hopefully this post has shown that even a simple button can be far more work that it initially seems.

My delicious.com bookmarks for January 15th through January 18th

  • ‘should be cheaper than free’ – "I’m angry at the customers who send me nasty emails or reviews, threatening me with ‘telling Apple to remove it’ or rating it 1 star with a ’should be cheaper than free’ remark because after paying the ridiculously exorbitant 99c, they found it didn’t live up to expectations. "
  • Hurdie Ho! – Maybe you had to be there and read the copy of Your Sinclair that this was originally published in, but this still makes me laugh.
  • Museum looks at 2000-year history of the computer – The Computer History Museum was excellent even before this new exhibit. Recommended.

www.cut 2.0

A new version of my handy URL shortening app for iPhone and iPod touch has just hit Apple’s App Store. Please go and download it — it’s free!

www.cut main screen

I’ve added to the services that were supported in the previous version and now www.cut supports nine shortening services:

Also new in version 2.0 is a “URL Scheme.” This is a technical name for something that’s very useful. Picture the scene: you’re in Safari and you want to send the current URL to www.cut. Previously you had to copy the URL to the clipboard and manually launch it. Now, all you need to do is add “wwwcut:” to the beginning of the URL and www.cut launches automatically.

For example, if you’re looking at “http://www.apple.com/”, you would edit the URL to be “wwwcut:http://www.apple.com/” and press “go.”

When I use www.cut, most of the time I’m going to send a link to Twitter or Facebook. So in this new version there is the option to do just that within www.cut. It’s available as an in-app-purchase and also removes the adverts.

Another change you might see is if you enter something that looks like a URL but, technically, isn’t one, www.cut will suggest a correction. Another example, say you enter “www.apple.com”. I know that’s what they put on the TV and posters, but it’s not really a URL. www.cut will ask if you really meant “http://www.apple.com”.

Unfortunately, URL shortening is a tough business and a couple of the services that were available in version 1.0 are no longer operating. This means that you can no longer use In the 2.0 release, tr.im or cli.gs. If you upgrade, www.cut will switch you back to using is.gd, but you can easily switch.

Giving Back

A few years ago I was very much “into” the whole open source movement. I read LWN (still do, actually). I bought a copy of The Cathedral and the Bazaar.

But one thing I never really did was contribute to open source projects. I never really had much need. They largely did what I wanted and when they didn’t, well, the modifications were too big to consider attempting in my spare time.

And now I have a couple of applications in Apple’s App Store. I get the impression that a lot of apps you see there these days are mainly collections of open source code bundled together with some glue code and some new graphics. I know it sounds bad when you phrase it like that, but I don’t mean it that way. When you’re coding to a deadline and to cost this is a perfectly valid way of doing things.

But neither Yummy nor www.cut are like that. They are almost entirely my own code. Why? Two main reasons.

The first is historical: Yummy was initially developed when the now infamous NDA was in place. Developers couldn’t share code even if they wanted to.

Secondly, I like to minimise external dependencies. The iOS platform changes fast enough as it is without having to wait for other developers to update code as well. Of course, open source means that I could fix it myself but that probably means looking at code I’m not completely familiar with and that takes time. The desire to remove third party code led me to remove AdMob in Yummy Browser and www.cut and replace with Apple’s iAd, even knowing that the fill rate was very low ((Early indications are that, even with the lower fill rate, income is not vastly different, and this is with only two countries active at the moment with iAds.)).

Ultimately, however, there’s too much great code out there to avoid it altogether. The current, shipping version of Yummy has two open source components and I have just released a (tiny) third one.

First is the Facebook Connect SDK. I have not made any modifications to this.

Second is InAppSettingsKit. This some software that duplicates the Settings screen and allows them to be included within the app as well as in the Settings.app. I made some minor fixes so that it works on the iPad (independently fixed and pushed upstream) and I added a “Log in to Facebook” cell. I’m not sure how common a requirement this is likely to be, so I’ve not pushed it back to the maintainers but I have made it available in a separate branch:

InAppSettingsKit

Third is something that should really be included in iOS itself. Apple’s guidelines say that when your app is sent into the background it should close any menus that are open. In “iPhone Speak” these are called UIAlertView (dialog in the middle of the screen) and UIActionSheet (menu from the bottom of the screen).

In addition, the iPad had a further requirement: there should be only one menu open at any given time.

I created UIViewAutoDismiss to help:

UIViewAutoDismiss

This code is inspired by a question and answer on Stackoverflow.