Getting the title of a Web View in Cocoa Touch

In an application I’m working on, I display a UIWebView with a navigation bar. I wanted to display the title of the web page in the navigation bar when I load a page, but there doesn’t appear to be any obvious way to do it.

There’s actually a very easy way to get the title (or any other property) of a page in a web view: stringByEvaluatingJavaScriptFromString:.

In this case, I use the delegate method webViewDidFinishLoad to obtain the page title and set the title of the navigation bar,

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString* title = [webView stringByEvaluatingJavaScriptFromString: @"document.title"];
    navbar.title = title;
}

The result looks like this:

iPhone Simulator
Uploaded with plasq‘s Skitch!

Adding more books to Coda

Coda is my favorite web development environment on the Mac, combining a syntax-aware editor, a visual CSS editor, and an FTP client that can sync local changes to the web server. Until I read this item at Dra Studio, I had no idea you could add custom books to Coda.

Adding books to Coda is very simple – simply click the ‘+‘ button at the bottom of the Books page and enter the book title, main URL, and a search URL. You can find URLs for some useful books to add at Dra Studio & SitePoint.

I find the WordPress documentation (codex.wordpress.org) especially useful when working on WordPress plugins or themes.

Coda
Uploaded with plasq‘s Skitch!

Handling multi-touch in an iPhone application

One of the changes I’m making in the next version of ICanHasCheezburger is improved multi-touch handling. In the current version, I’m only handling left & right swipes to switch to the previous or next images. For the update, I’d like to handle the same gestures as the photo app:

  • Pinch to zoom in & out
  • When zoomed in, drag the image to see different portions
  • When zoomed or moved, double-tap to return to the standard zoom
  • Swipe left & right to move to the next image – but NOT when zoomed in
  • Single tap to toggle toolbar & menu bar

In many cases you can use a UIScrollView, which implements most of those behaviors. For ICHC, I decided to subclass UIImageView to handle touch gestures.

Read more

The D90 is selling fast

I like to check Flickr’s camera finder page every few days to see how popular the D90 is. As I write this, the D90 is ranked 15 of 102 Nikon models. Less than a week ago, it wasn’t even listed. Yesterday it was #19, a jump of 4 places in one day.

Here’s a shot I took today with my 50mm lens at f1.8. I love being able to use auto focus with that lens.

New Orchid

Using a WebKit view for help

Since the F**KING NDA makes it difficult to find iPhone programming tips & sample code, I thought I’d share this little tidbit.

Sometimes the easiest way to display a help or information screen is a UIWebView. It’s very easy to display the contents of an HTML file stored in the application’s resource bundle.

    NSData *info = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"info" ofType:@"html"]];
    [webView loadData:info MIMEType:@"text/html" textEncodingName:@"utf8" baseURL:nil];

However, if you have any external links, they will open in the same web view, which you probably don’t want. I found a very nice work-around to have any clicked URLs open in Safari. You’ll need to specify a delegate for your web view that implements the webView:shouldStartLoadWithRequest:navigationType method. The code is very simple.

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if (navigationType == UIWebViewNavigationTypeLinkClicked) {
        [[UIApplication sharedApplication] openURL: [request URL]];
        return NO;
    }
    return YES;
}

I love when things suddenly fall into place

For the past few months I’ve been working on a server administration application (I can’t go into specifics). It was ported Windows code that included some MFC emulation using old-fashioned Mac APIs such as WaitNextEvent. As expected, it never worked reliably and I was starting to feel defeated by it.

I figured out that the actual server code consisted of self-contained C++ objects that had very little to do with the application code. I ended up throwing away most of the old application code and grafting the server code into a modern Cocoa application using some ObjectiveC++ bridge classes. Once I got it to build, it took less than a day to get it working, and in the process fixed lots of long-standing bugs.