Tuesday, 14 September 2010

iPhone SDK: Running a selector after a delay..

Quite often I've needed to run a selector after a certain time interval. Usually what I've done is the following:

[NSTimer scheduledTimerWithTimeInterval:0.0 target:self selector:@selector(doIt:) userInfo:nil repeats:YES];


A shorter and slightly easier method is a selector on NSObject (now that's a base class with a lot of methods if ever I've seen one!)

[self performSelector:@selector(doit) withObject:nil afterDelay:1.0];


If you set afterDelay to be 0.0, you have a quick and easy way of making a selector run once the current run loop has completed.

Thursday, 9 September 2010

iPhone SDK: What NSURL methods actually return

Apple's NSURL documentation has very little detail on what exactly the various NSURL methods actually return. It just says unhelpful things like 'returns the path' - what path?

Here's an example NSURL, and the output you actually get when using some of the methods.

NSURL* url = [NSURL URLWithString:@"http://username:password@www.example.com:888/folder1/folder2/filename.html?query1=value&query2=value#anchor1"];
[url scheme] -> "http"
[url user] -> "username"
[url password] -> "password"
[url host] -> "www.example.com"
[url port] -> "888"
[url path] -> "/folder1/folder2/filename.html"
[url lastPathComponent] -> "filename.html"
[url pathExtension] -> "html"
[url fragment] -> "anchor1"
[url absoluteString] -> "http://username:password@www.example.com:888/folder1/folder2/filename.html?query1=value&query2=value#anchor1"

Wednesday, 8 September 2010

Mac OSX Terminal: Recursive list of files in directory excluding .svn and .DS_Store folders / files

Those pesky .svn and .DS_Store files and folders are the enemy of many a terminal command..
Here's how to get a handy list of all files in a folder without them, in a simple format for later processing.

find * -type f | grep -v "\.svn" | grep -v "\.DS_Store"


Outputs:
App-Info.plist
Default.png
GpsDrawTheme/theme.json
RootModule/code/common.js
RootModule/code/db.js
RootModule/code/drawing.js
RootModule/code/drawingMenu.js
RootModule/code/index.js
RootModule/code/instructions.js
RootModule/code/myDrawings.js
.. etc


I'm sure there's a way this could be simpler, but this works for me.

Monday, 6 September 2010

HTML5 Local Storage

There are a number of different technologies for storing data locally in the web client. Cookies, SQLite (in those browsers that support it), the offline cache manifest, and also 'Local Storage'. If you simply want to store some name-value pairs, such as user config data, local storage is by far the simplest option. Up until today we had been using Brian le Roux's lawnchair for local storage, which we used in the mode where it wraps SQLite (specifically the Webkit version). The fact that all requests were asynchronous, while nice in most situations, didn't fit our model. So I've switched to localStorage instead, which has the added bonus of being ridiculously simple.

var text = localStorage.getItem("foo");
if (text === "hello") {
alert("You've been here before!");
} else {
localStorage.setItem("foo", "hello");
}


The system doesn't support storing javascript objects or array though. Happily, this is easily fixed with a combination of JSON.stringify and JSON.parse to move the object to and from a string representation.

var st = localStorage.getItem("foo");
var ob = JSON.parse(st);
if (ob && ob.text === "hello") {
alert("You've been here before!");
} else {
var toSave = { text:"hello", other:[1,2,3] };
localStorage.setItem("foo", JSON.stringify( toSave ) );
}


You can clear the contents by using the localStorage.clear() method.

There's a simple demo HTML5 local storage in action available here

Wednesday, 1 September 2010

MacOSX: Opening documents, folders, and applications from the command line

The Mac OSX command line has a very handy 'open' cp,,amd which tells the Mac OS UI to open the item you pass in. This is great for switching from using the terminal to using the UI for those times when the UI is more convenient.

Open the current folder in Finder
open .


Open a file using the default application
open mydoc.txt


Open a file a specific application
This is the same command as right-clicking on an item in finder and choosing 'Open With'. It can be really handy for testing an html file in a specific browser.

open -a /Applications/Safari.app index.html