Matt Langtree

What Drives Me to Develop Software

macdrifter on Developers, A Love Story:

If I like a developer I buy their wares just to support their work. When I say “I like a developer” I don’t just mean I like their products. I mean that I like the people behind the products.

The reasons he highlights in this article are the same reasons why I develop software.

Some People Write Newsletters. I Collect Links.

This is the first in hopefully a series of many blog posts about the resources I come across each month. For a long time I have been collecting useful links I have found around the internet and stored them on the code/link/image sharing service Medium Stack. Here is my list for January 2012.



All of these links come highly recommended. Let me know on Twitter (@mattlangtree) if you you liked this post or have any feedback.

Entity Relationship Diagrams From Sequel Pro

I found a great set of instructions for generating an Entity Relationship Diagram (ERD) from Sequel Pro indirectly (using the .dot format) in this article: http://norbauer.com/notebooks/code/notes/erd-diagrams-from-sequel-pro

I adapted the instructions that JD posted in his article to generate a PNG image of the ERD of a database.

Here are the instructions in full:

  • Download the latest version of Sequel Pro
  • Install graphviz via homebrew (note - sudo isn’t required for my installation of homebrew). brew install graphviz
  • Connect to your MySQL database using Sequel Pro.
  • In Sequel Pro, go to File > Export… > Choose Dot as the export type > Export.
  • Launch Terminal.app and run the following command from the directory where the .dot file was exported to dot -Tpng your_database.dot > your_database.png 
  • I found that on a larger database that this produced a 5355 × 3784px image (which is probably great for printing out on A3 paper)
  • I was looking for an ERD I could print out to pass around to some other developers, so I opted for the fdp binary to give me the best results. Again in Terminal.app run: fdp -Tpng localhost-ios-15-11-11.dot > your_database.png 

My instructions vary from JD’s a little in that I use homebrew for installing graphviz and that I export using a different binary (included in the graphviz package). I also took the shortcut of exporting to PNG directly rather than converting to an SVG file first.

Self-signed SSL Certificate on Mac OS X Lion

I needed to install a self-signed SSL Cert on my local Mac OS X Lion machine with the built-in Apache. You might consider doing this yourself if you ever want to write code that needs to utilise a secure HTTPS connection without having to deploy to a testing/live server for testing with a valid SSL certificate.

The following link helped quite a bit:

http://www.houseofding.com/2008/11/generate-a-self-signed-ssl-certificate-for-local-development-on-a-mac/

The instructions don’t directly match the Lion install, and I ended up putting my apache config options in /etc/apache2/extra/httpd-ssl.conf.

Having a bit of experience with setting up Apache and mod-rewrite previously, it didn’t take longer than 30 minutes to work out - including using my custom URL namespace:

https://myfancysite.mac/

My only suggestion is that if you are sure you’ve done everything right but you can’t get Apache to start then try typing the following command to test for syntax/startup errors.

sudo apachectl configtest

UITableView Section Header Draw Issue in iOS5

If you are using custom section header views in your UITableView you would have implemented UITableViewDelegate’s following two delegate methods:

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section

If you are using the same code for tables with and without section headers there’s a good chance that you have some logic like the code below. The important point to note with the release of iOS5 is that it is no longer suitable in iOS5 to only return nil on the - (UIView ) tableView:(UITableView )tableView viewForHeaderInSection:(NSInteger)section if you don’t want the section header to show. You now need to explicitly set a height of 0 for the section header.

If you don’t follow this rule, you may end up with an empty section header being drawn, like so:

New UITableView code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
  // This is the new important bit for iOS5.
  if ([self currentViewNeedsTableHeaders] ) {
      return 0;
  }
  
  return 45;
}
// Will produce a green view with a black bottom separator line.
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
  if ([self currentViewNeedsTableHeaders] ) {
      return nil;
  }

  // How wide is the screen?
  float w = [[UIScreen mainScreen] bounds].size.width;
  headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0,0.0, w, 44.0)];
  headerLabel.text = [self tableView:tableView titleForHeaderInSection:section];
  headerLabel.backgroundColor = [UIColor colorWithRed:0.431 green:0.820 blue:0.137 alpha:1.0];
  headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, w, 45)];
  headerView.backgroundColor = [UIColor blackColor];
  headerLabel.font = [UIFont boldSystemFontOfSize:15];
  
  // Add Label to the headerView's view hierarchy
  [headerView addSubview:headerLabel];
  return headerView;
}
- (BOOL)currentViewNeedsTableHeaders
{
  return ([self.someProperty isEqualToString:@"parrot"]) ? FALSE : TRUE;
}

iPad Large Table Data [Sample Project]

So you have a large XML feed with lots of rows/columns that you want to turn into an iPad app. Let’s see what options we have for presenting this data.

  1. Start with UITableView and create a subclass of UITableViewCell to layout all of the columns. Sure, but maybe you’ve got more columns than you can show on a single iPad screen. UITableView is not going to be an elegant solution here.
  2. OK, you’ll just hook up an NSTableView right? ba-bum! — It’s Mac only.
  3. Maybe you could grab Daniel Tull’s DTGridView and add a bunch of UILabel’s to a grid view. This is pretty good, but there is an easier way..
  4. Try a UIWebView and use HTML, CSS and Javascript? Well this is the easiest of the lot, so let’s give this a go. What I have created is a hybrid native/web app. We take an iPad app shell, throw in a UIWebView, and use HTML, Javascript, and some CSS to come up with a great looking table of information. I have used HTML 5, jQuery,  Twitter’s Bootstrap and Last.fm’s XML API for events at venues.

{ % img http://mattlangtree.com.au/wp-content/uploads/app-breakdown1.jpg % }

The example project DataTable isn’t modelling a particularly large dataset, but it’s a good start. Additionally, you can use this as a reference for future iPad web apps.

DataTable: View on GitHub | Download Source (Requires Xcode 4.1 or higher)

DCIntrospect - Debugging Views in iOS (Update: Video)

DCIntrospect is a tool that I have found to be invaluable to my work/personal projects recently.

From Pat’s project description:

Introspect is small set of tools for iOS that aid in debugging user interfaces built with UIKit. It’s especially useful for UI layouts that are dynamically created or can change during runtime, or for tuning performance by finding non-opaque views or views that are re-drawing unnecessarily. It’s designed for use in the iPhone simulator, but can also be used on a device.

If you are laying out custom views and tracking their position/movement throughout the lifecycle of your app this tool will be quite useful.

I heard about this project via word of mouth a while ago, and a few Swipe Conference speakers were speaking very highly of it.

Three things to check out in the included demo app (and to use in your app):

  1. Hit spacebar to activate DCIntrospect’s inspector in the iOS simulator.
  2. Select any view in your app and press the ‘p’ button on your Mac’s keyboard to dump the properties of that view
  3. Use the arrow keys on your keyboard to nudge views around whilst your app is running (similar to Webkit’s Web inspector) Example of properties logged by DCIntrospect’s inspector:
Output from DCIntrospect
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
** UIImageView : UIView : UIResponder : NSObject **
** UIView properties **
tag: 0
frame: {{147, 20}, {64, 64}} | bounds: {{0, 0}, {64, 64}} | center: {179, 52}
transform: [1, 0, 0, 1, 0, 0]
autoresizingMask: UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin
autoresizesSubviews: YES
contentMode: UIViewContentModeScaleToFill | contentStretch: {{0, 0}, {1, 1}}
backgroundColor: R: 0 G: 0 B: 0 A: 0.00
alpha: 1.00 | opaque: NO | hidden: NO | clips to bounds: NO | clearsContextBeforeDrawing: YES
userInteractionEnabled: NO | multipleTouchEnabled: NO
gestureRecognizers: nil
** UIImageView properties **
drawMode: 0
image:
highlightedImage: nil
animationImages: nil
highlightedAnimationImages: nil
animationDuration: 0
animationRepeatCount: 0

Melbourne Cocoaheads July ‘11 - Talk 2 - Patrick Richards - DCIntrospect from Oliver Jones on Vimeo.

iOS Libraries and Frameworks I Use — September 2011

When I started out learning iOS application development I wished that there was a list of libraries that were proven to work well, were well documented and had great coverage on Stack Overflow.

Here is the list of libraries and frameworks I use with great success in my projects:

ASIHTTPRequest — A perfectly decent library for all of your HTTP requests including downloading the content of a URL or submitting a form to a website API/endpoint. (Alternatives: GTM HTTP Fetcher thanks @neilang)

SBJSON — A great JSON parser and generator. Turn JSON into NSDictionary’s NSArrays (etc) and vice versa.

Apple’s Reachability class — Included in ASIHTTPRequest, but worth a mention. Apple provides a class for detecting whether you have 3G/Edge access and the device is capable of routing IP traffic. It also pings Apple’s servers to check if you have “greater internet” availability.

EGOImageView — If you are working on a web-based app and you need to show a tableview full of web-loaded images you will want a library like this.

MBProgressHUD — A fairly easy to implement class for showing a translucent HUD (Heads Up Display). Use this if you want to notify a user that an operation has completed or the progress of some database operations that are running.

SFHFKeychainUtils — Fairly straightforward class for interacting with iOS’ keychain. If you are storing user’s passwords in NSUserDefaults – stop. Use the keychain and give this class a go.

MOC/NSManagedObject+Additions — Custom proprietary class for handling core data interaction.  Written by @abhibeckert with additions from yours truly. If you are using or have written a very light weight Core Data wrapper, let me know in the comments.

If you have tried any of these and have found an alternative that works better let me know in the comments.

Images

Mac OS X Lion - Turn Off Saved Apps

Situation: I have a late 2009 iMac (3.06Ghz Core 2 Duo, 4GB Ram, No SSD) running Mac OS X Lion 10.7.1 in the office. It’s a fast machine — On Snow Leopard.
The Problem: After a day of work, my iMac gets shut down at 10pm to save power and starts up again at 7am. The process of logging in to my Mac in the morning is quite a slow one, with the addition of the new “Restore windows” feature in Mac OS X Lion.
The Solution: Any of my machines with non-SSD drives running Mac OS X Lion, now have “Restore windows when quitting and re-opening apps” disabled by default.
You can do this by going into System Preferences > General and unticking the “Restore windows when quitting and re-opening apps” option.

I would recommend that if you are running an older iMac without an SSD and you log in/out of your machine on a regular basis that you follow my lead  — At least until the option of installing an SSD in your machine is a feasible option (a highly recommended option).

Restarting a Video in MPMoviePlayerController

I’m working on a project where the standard controls provided by MPMoviePlayerController aren’t matching the existing styling of the app design. I need to provide a custom rewind button for the movie I’m auto- playing for the user. When the video finishes it needs to hide the modal movie view controller, and I’m handling this with the MPMoviePlayerPlaybackDidFinishNotification notification. The best way I can think of doing the rewind without triggering the view to dismiss is with the following code..

Restart video in iOS
1
2
3
4
5
6
7
8
9
10
11
12
- (void)restartVideo
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];
    [mp stop];
    [mp play];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(moviePlayBackDidFinish:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];
}

Update: Thanks to @avenjamin for suggesting that I call the method -restartVideo rather than -rewindVideo as the meaning was slightly incorrect.