My iOS Bookmarklets

May 17, 2011

Bookmarklets are links you can add to your browser toolbar that perform different functions based on the page you’re currently on. They’re the only option for extending the functionality of Safari on iOS.

I’ve added my favorites to a page with instructions linked below. (WordPress for iPad breaks bookmarklets, it seems.)

My iOS Bookmarklets

iOS Monospace Font Bug – A JavaScript Fix

December 20, 2010

After struggling through trying to find solutions for the iOS monospace font problem, I was able to cobble together a JavaScript fix.

To summarize the problem: in fixed-width fonts on iOS, spaces, question marks, and hyphens/dashes all display wider than letters, numbers, and other symbols. Replacing the dashes with –es fixed them, but there was no HTML entity fix for question marks or spaces (all the special spaces display either wider or narrower than regular characters).

I was experimenting with different ways to try to get the characters the same width, and I was going to try wrapping the “bad” characters in a span so I could style them differently. What I found was that characters wrapped in a span display at a different width than either the “normal” or “bad” characters not in a span, so it didn’t serve my purpose. But, I also noticed that (almost) all characters (both “normal” and “bad”) displayed at the same width when they were placed into spans!

Incidentally, spaces still didn’t display at the right width, nor did any of the special space HTML entities. (It seemed inconsistent–spaces seemed OK on iPad, but not on iPhone.) The best solution I’ve found so far (and it’s not a good one for the sake of copy-pasting text) is to replace the spaces with an underscore, and style the spaces to be non-visible.

So a solution would go through my code to output, take all the text (non-HTML) characters, and wrap each of them in a span. This was easier on the client-side than the server-side, because the browser has already parsed my document into DOM objects, so it’s easy to tell what’s plain text characters.

The JavaScript function I wrote takes a node, and iterates through its child hierarchy. Whenever it finds a text node, it splits it into characters, wraps each in a span, performs the extra steps I described above for spaces, and then inserts these spans into the DOM in place of the original text node.

Another limitation of this setup is that it can’t be used for editing text in textareas–it can only be used for displaying text.

Note: this has been tested very little! No warranty it won’t erase all your data. If you find cases where it doesn’t work, and you can make it better, let me know and I’ll post the updated version here.

Download the source, or check out an example of it in action, which allows you to see the text before and after the fix is applied.

Monospace Fonts on iOS

December 20, 2010

Today I just had what might be my biggest disappointment of the year: monospace fonts are not monospaced on iOS. Specifically, the hyphen and question mark are wider than other characters in Courier New on iOS. Of course, this completely messes up any kind of monospace-based layout you might want to do.

Here are the alternatives I’ve investigated that didn’t work:

  1. Leaving the programming industry. Unfortunately, I’m not smart enough to do anything else. On to more reasonable alternatives.
  2. Tried other installed fonts. Courier is supported, but has the same problem. Monaco isn’t supported. And that’s all the built-in font options.
  3. Web fonts. I searched through the Google Font Directory and found “Droid Sans Mono.” If that had worked, man, what a slap in the face, huh? Unfortunately, it exhibited the same problem.

Here are options that could or did work:

  1. In app store apps, custom views to lay out text. If you draw one character at a time, you can make sure they’re placed properly. Unfortunately, if you need to use this for an editor (as I do), that would mean re-implementing a ton of functionality.
  2. On the web, replace dashes with HTML entities. This works for webapps and UIWebViews. If you replace all dashes/hyphens with –, you will see an identical (in Courier New) looking dash, that is spaced properly. This works for displaying text, but wouldn’t work for editors, which are just displaying bare characters and not interpreting HTML entities. Also, this doesn’t solve the question mark problem.

Does anyone else have any suggestions?

External CSS and JS in a UIWebView

November 1, 2010

When you’re displaying content in your iPhone app in a UIWebView, it’s helpful to be able to store your JavaScript and CSS in external js and css files. But the way to access these reliably has changed as iOS has matured, and there’s a lot of outdated misinformation about it online. Here’s what worked for me.

By the way, this assumes you’re programmatically creating the content shown in your UIWebView, so this solution wouldn’t work if you’re accessing external web sites. But if you’re doing that, they’ll probably be referencing external CSS and JS anyway. Also, my app doesn’t need to display images in the UIWebView, so I haven’t looked into a solution for displaying local images.

As of iOS 3.0, it appears that a UIWebView can no longer access local files directly. But you can get around this by reading in the contents of the files and then writing them into the HTML of the page you’re going to display. This means you can still store the CSS and JS in their own files, which is much better for readability.

Here’s the code snippet to pull it in: (you may need to copy-paste to see it all)

// load css styles
NSString *cssPath = [[NSBundle mainBundle] pathForResource:@"MyCSS" 
NSData *cssData = [NSData dataWithContentsOfFile:cssPath];  
NSString *cssString = [[NSString alloc] initWithData:cssData

// load js
NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"MyJS" 
NSData *jsData = [NSData dataWithContentsOfFile:jsPath];  
NSString *jsString = [[NSString alloc] initWithData:jsData

// compose full html page
NSString *pageContent =
[NSString stringWithFormat:@"%@%@%@",
 cssString, jsString, actualPageMarkup];

If you try this, the page should display with your CSS, but the JS won’t work. Why? Because, by default Xcode 3.2 thinks your JS files should be compiled, and it doesn’t know how to do it. Instead, what you want is for Xcode to just include the JS files in your bundle, just like it does with the CSS files. To do this, go under Targets in your left-hand “Groups & Files” sidebar, then under your app, then under “Compile Sources.” You should find all your JS files under there. Drag them under “Copy Bundle Resources” instead, rebuild, and the JS should run just fine.

Stupid Cocoa Bugs: Bad Access

October 27, 2010

That is to say, bugs in my Cocoa code that are stupid–not stupid bugs in Cocoa itself =]

After updating something trivial in my iPhone app, I started getting EXC_BAD_ACCESS crashes on the device–but only when it wasn’t hooked up to the debugger. I found out that the error happened in the dealloc method of one of my UIViewControllers, on a line where I release one of the object’s property objects. I checked all my code and it seemed like I was following standard memory management rules.

The solution ended up being dumb: at some point I had gotten it into my head that I was supposed to call the superclass’s dealloc method, then release the properties:

-(void)dealloc {
    [super dealloc];
    [myProperty release];

But, when I looked in the Memory Management Programming Guide, it seems that you’re supposed to release your properties first:

-(void)dealloc {
    [myProperty release];
    [super dealloc];

This makes sense, assuming that at some point up the superclass chain the object itself gets released or free’d.

The crazy thing was that I’d used the wrong order for the app for over a week without any crashes. I guess this is part of what a JVM guy like me needs to learn about memory management: the problems don’t always show up reliably or right away.

Different CSS and JS for PC, iPad, and iPhone

October 17, 2010

I’m working on a webapp that will run on PCs, iPad, and iPhone. To get the optimum experience for each, I have some styles and JavaScript that I want to run on each, some that are only for iPad, some only iPhone, and some for a combination of the above. Here’s the patterns I’m using to implement this.

JavaScript is easy: I just use regular user agent detection to determine which platform we’re on.

function iOS() { return null != navigator.userAgent.match(/(iPad|iPhone)/i); }
function iPad() { return null != navigator.userAgent.match(/(iPad)/i); }
function iPhone() { return null != navigator.userAgent.match(/(iPhone)/i); }

CSS is a bit more complicated, but not much. I’ve found recommendations for how to target CSS specifically to iPad and iPhone. But really the iPad one is for all iOS devices, so I end up with an iOS CSS file and an iPhone CSS file:

<link rel="stylesheet" type="text/css" media="only screen and (max-device-width: 1024px)" href="iOS.css" />
<link rel="stylesheet" type="text/css" media="only screen and (max-device-width: 480px)" href="iPhone.css" />

So I put my PC styles in my standard stylesheet. Then, I fill iOS.css with styles that I want on all iOS devices or only on iPad. Then I fill iPhone.css with styles I want only on iPhone; and, if there are any styles in iOS.css that were for iPad only, I overwrite them in iPhone.css. For this to work, I have to list iOS.css before iPhone.css, as I did above.

iPhone Development Options

March 9, 2010

Either I’m behind the times, or Apple isn’t great about communicating all the options out there for iPhone development. I just found out about Dashcode, an iPhone SDK for creating web apps with all the standard awesome iPhone widgets. It even lets your web app run offline! This being the case, I thought I’d put together a summary of the options you have for creating iPhone web sites/apps. Please comment if I’ve gotten anything incorrect, and help me out by providing better links =]

Web Site iPhone Site Offline Site App Store
Accessible y y y y
Other Mobile y y ? n
Offline n n y y
Zooming y n n n
App Store Approval n n n y
App Store Sales n n n y
iPhone Data n n n y
Push n n n y
  • Web Site – This refers to a regular web site, without any adjustments for the iPhone.
  • iPhone Site – This refers to a web site with some changes for the iPhone. It might be as simple as a few meta tags and CSS styles, or you might have to rewrite your views to be iPhone targeted. Either way, though, it’s not much effort. More Info
  • Offline Site – This refers to a webapp that’s set up to be able to run offline. If your users save your app to the home screen, everything on that page is saved as well, including JavaScript. If the app is set up to use a JavaScript-accessible local storage library, that data will be accessible as well. Apple has created an SDK called Dashcode that offers not only offline storage, but also easy drag-and-drop usage of regular iPhone UI widgets as well. More Info
  • App Store – This is what most people think of when they think of iPhone apps. These are written in Objective C using the iPhone SDK. They require App Store approval to be sold. More Info

PushMail Settings for Google Voice

December 4, 2009

I use PushMail to get push notifications of e-mail on my iPhone. And because I have Google Voice send all my SMS and voicemails to my e-mail address, I get notified of those too. Here are some PushMail Custom Profiles that make it an even better experience:

Custom Profile 1 – Text Messages

  • Pattern:
  • Sound: SMS Received
  • Action: URL:
  • Notification Format: From, Body

Custom Profile 2 – Voicemails

  • Pattern:
  • Sound: Ping (default)
  • Action: URL:
  • Notification Format: Subject

My Current iPhone App List

November 30, 2009

In approximate order of how often I use them, or how useful I find them.


  • PushMail – push notifications of e-mails, including preview text (great for getting free instant notifications of Google Voice SMS and voicemails, instead of using SMS)
  • Tweetie – twitter
  • Outliner – outlines, especially hierarchical to-do lists
  • IM+ – instant messaging
  • GPS Drive – cheap GPS program with some improvements over Google Maps
  • Wedding Day – now, how long we’ve been married =]


  • Files lite – reading text files, such as videogame walkthroughs (it saves your place!)
  • AudioMemos Free – voice memos (better UI than Apple’s Voice Memos app)
  • ICHC – funny images
  • NetNewsWire – Google Reader / RSS
  • The Weather Channel – detailed weather forecasts and radar
  • Logos – free ESV, NASB, NKJV Bible – plus Greek morphology!
  • Pandora – customized streaming music stations
  • Pollen Lite – pollen forecast
  • Loopt – for tweeting my current location
  • iTweetSong – tweeting the song/podcast I’m currently listening to
  • Flixster – movie showtimes
  • Fandango – buy movie tickets (but smaller list of theatres and worse UI than Flixster)
  • Shazam – recognize a song playing on the radio
  • PS Mobile – simple photo manipulation
  • WhitePages – look up a person’s address
  • PhoneFlicks – view/update my Netflix queue
  • ESPN ScoreCenter – for my wife to look up Broncos scores
  • Dictionary
  • eBay
  • Facebook
  • Wikipedia

My iPhone Setup

October 10, 2009

I just finally activated my Google Voice account today. I’m pretty happy with the setup right now. The bottom line is:

  • I get a notification on my iPhone for all e-mails, voicemails (transcribed), text messages (free), IMs, and twitter replies and DMs
  • I can e-mail, text message, IM, or tweet back from the iPhone

Here’s the setup:

  • My personal e-mail address is a POP account with my ISP. I have it set up to forward to my gmail account, so I get IMAP support. But I also have a backup of the e-mails stored locally in case something goes wrong with gmail.
  • My Google Voice account is set up to transcribe voicemails, and then send voicemail transcriptions and text messages to my gmail account. Gmail is set to filter these messages and automatically put them in a VM or SMS folder.
  • My gmail account forwards all e-mails to my PushMail e-mail account – this is an iPhone app that will show push notifications on my iPhone with sender/subject/title/body etc. By contrast, using the iPhone mail app to do push only buzzes and updates the app’s icon – it doesn’t show a notification with the contents.
  • Meanwhile, I use IM+ as an iPhone IM client. It supports most IM services, and also does push notifications when receiving IMs.
  • I use Tweetie 2 as my iPhone twitter client. It’s great, but unfortunately it doesn’t support global push notifications for all mentions and DMs. So, instead, I use IM+’s Twitter push support – IM+ notifies me when I receive a mention or DM, and then I read/reply in Tweetie.