Get Total and Free Space on the Mounted File System
I recently was writing code to manage the download and storing of mp4 files to enable an application to play movies off-line. One consideration when dealing with files of significant size (50-100 megs) is disk space availability – this tip shows the method I called to get information about the mounted file system.
Get Free Disk Space
To get free space available, the class NSFileManager provides a method attributesOfFileSystemForPath:error: which will return a dictionary that includes five entries with various file system information.
Continue reading this post »
How to Crop an Image
This post shows an example of one way to crop an image. Let’s begin by looking at a screenshot of the original and cropped image on the iPhone simulator:

As you can see, I am cropping a rectangle from the middle of the image on the top left. The code below shows how to accomplished this:
Continue reading this post »
Display YouTube Videos Without Exiting Your Application
If you’d like to play a YouTube video inside your application there are two common ways to do this, by launching the YouTube player and by using a UIWebview.
Launch Native YouTube Application
This approach will exit your application and begin the YouTube player on the iPhone:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=gczw0WRmHQU"]];
Convert an Image (UIImage) to Grayscale
A trick you won’t need often, yet when the day arrives, you’ll have this up your sleeve – a quick method to convert a UIImage object to grayscale.
Continue reading this post »
Screen Capture using UIGetScreenImage
Editor’s Note: I read today (7/22/2010) that Apple is no longer approving applications using UIGetScreenImage. I haven’t been able to find an official Apple announcement on this – however, please give this consideration before using this method in your apps.
As talked about on TUAW, among other popular blogs, Apple has opened up the API to allow applications to capture the screen contents. So what’s the excitement all about? Read on, as this method opens up some interesting possibilities…
Continue reading this post »
Showing Activity Indicator (spinner) in Table Cells
Following are two ways to display UIActivityIndicatorViews in a cell of a table view (UITableView). The first will add a spinner as an accessory view, which will display the spinner on the right of the cell. The second approach will add a spinner as a subview in the cell. The primary advantage of using a custom view is that you can display the spinner wherever you like in the cell.
Continue reading this post »
Adding an Activity Indicator (Spinner) to Navigation Bar
In addition to the standard buttons that you can place on a navigation bar (UINavigationBar), adding an activity indicator (UIActivityIndicator) can be helpful when you need a standard location to indicate some type of action is underway, for example downloading a file, performing a search…
You add an activity indicator through a custom view as shown here:
Continue reading this post »
The Basics of Protocols and Delegates
Apple offers a good overview of working with protocols in their Objective-C Programming Reference. However, sometimes a simple working example can go a long ways…
Introduction
Protocols can be helpful in a number of scenarios, a common usage is to define methods that are to be implemented by other classes. A familiar example is when using a tableview, your class implements the cellForRowAtIndexPath method which asks for cell content to insert into a table – the cellForRowAtIndexPath method is defined within the UITableViewDataSource protocol.
Let’s walk through a very simple example of defining and adopting a protocol.
Continue reading this post »
Tracking Down EXC_BAD_ACCESS Errors with NSZombieEnabled
It’s only a matter of time before you’ll find yourself face to face with the dreaded EXC_BAD_ACCESS error message. This message generally occurs when you attempt to access an object that has previously been released.
NSZombieEnabled is an environment variable which can be helpful to track down the elusive object causing the problem. With the environment variable set, Cocoa will redirect all objects isa pointer to an NZZombie object once an objects retain count has reached zero. In the future, should you try and send a message to a previously released object, an exception will be raised showing a message similar to the following:

Setting NSZombieEnabled Environment Variable
In the Groups and Files section, expand the Executables section and right click on your app name and choose Get Info from the dialog:

Select the Arguments tab on the top and then add a new entry in the Variables to be set in the environment section. Name the new variable to NSZombieEnabled and set its value to YES.

Tracking Down the Error
Using a zombie, messages sent to an object that has already released will now generate the exception shown above, and following the stack track should now help you find the location of the object that is generating the EXC_BAD_ACCESS error.
Don’t forget to remove the environment variable, or at a minimum untick the checkbox once you find the source of the problem.
iPad: Managing Multiple Launch Images aka Default.png Files
When loading an application on an iPhone or iPad, a launch image can be shown to provide feedback to the user that the application is loading. On the iPhone one Default.png file was adequate, with the iPad one needs to anticipate the device being started in any orientation, including upside down.
iPad Launch Image Orientations
To deal with various orientation options, a new naming convention has been created for iPad launch images. The screen size of the iPad is 768×1024, notice in the dimensions that follow the height takes into account a 20 pixel status bar.
| Filename | Dimensions |
|---|---|
| Default-Portrait.png * | 768w x 1004h |
| Default-PortraitUpsideDown.png | 768w x 1004h |
| Default-Landscape.png ** | 1024w x 748h |
| Default-LandscapeLeft.png | 1024w x 748h |
| Default-LandscapeRight.png | 1024w x 748h |
| Default.png | Not recommended |
* If you have not specified a Default-PortraitUpsideDown.png file, this file will take precedence.
** If you have not specified a Default-LandscapeLeft.png or Default-LandscapeRight.png image file, this file will take precedence.
Although you can include a Default.png file, and it will be used if no others are specified, I would consider it a best-practice to include all the relevant images needed by your application.
iPad Startup Orientation
Even though an iPad app may be started in any orientation, Apple recommends that you configure your application views upon startup in the preferred orientation inside the application:didFinishLaunchingWithOptions: method. Then, once the system notifies your application of the current orientation, you can arrange your views as needed.
Rename an Xcode Project
If you’ve ever attempted to rename an Xcode project you can speak first hand to the pain of getting all the details just right. Good news, it was recently pointed out by a reader in this post: Change Company Name from Within Xcode on a Per Project Basis that there is now a rename feature built into Xcode.
Renaming an Xcode Project
First, click on the Project menu and select Rename:

From there, fill in the new name, I’d also recommend creating a snapshot as a precaution:

Renaming an Xcode project is no longer a mysterious, troublesome endeavor.
Read Files from the Application Bundle
Below are a few lines of code to get you started if you need to open and read a file that is stored in the application bundle. Files could be anything from help text that your application displays to specific content that facilitates testing.
As an example of the later, often times I find it helpful to put json (or xml) content into a file that I know is valid based on a remote server that I will eventually connect with. I add this file to the application bundle and read it into an internal data structure. I can then do various tests with known data, and once that is working, I can then proceed to the networking side of things to connect and retrieve live data.
// Read in and store as string NSString *file = [[NSBundle mainBundle] pathForResource:@"jsonTest" ofType:@"txt"]; NSString *str = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:NULL]; debug(@"str: %@", str); // Do something with the test data... // Read in and store as raw data bytes NSString *file2 = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"txt"]; NSData *data = [NSData dataWithContentsOfFile:file2];
Overriding NSObject Description Method
No doubt NSLog is your friend, I use it frequently to print messages to the console during development. Actually, I use a variation of the NSLog that prints messages to the console sans date, time and object information, you can read about my approach here: Yet Another Debug Output (NSLog Replacement)
In this tip I’ll cover something similar as it relates to printing debug information to the console, specifically, overriding the description method of an object to provide information about instance variables.
Continue reading this post »
Voices That Matter iPhone Developers Conference – Free Pass Winner Announcement!
Voices That Matter and iPhone Developer Tips are happy to announce we have a winner in the drawing for one free pass to the iPhone Developers Conference in Seattle, Washington!
Voices That Matter is a two-day event that presents sessions covering the latest and most intriguing technology in application development for the iPhone and the iPad. A partial list of topics includes:
* Killer User Interfaces for iPhone OS Devices
* Core Animation
* iPhone OS Game Programming
* Audio and OpenAL
* Core Data for the iPhone OS
* Memory Management
* Xcode
Winner of the Free Pass

Paul Herrick, a Sales Manager South America for Brokk Sweden is the winner of the free pass to the iPhone Conference. Paul is just getting started with iPhone application development so no better opportunity to ramp up quickly than hanging with other iPhone developers. Congratulations Paul.
Thank you to everyone who entered in the drawing.
Change Company Name from Within Xcode on a Per Project Basis
A common question for most anyone new to Xcode is how to change the Company Name that is added to each new source file. The default information looks similar to the following:
Copyright (c) 2010 MyCompanyName. All rights reserved.
Up until Xcode 3.2 to change this value you had to resort to running a command from a terminal, you can see how this was previously done in this tip: Change Company Name in Xcode.
A welcome change with 3.2+ is that this value can now be managed from within Xcode. Even better, the name can be configured on a project by project basis, which is really nice if you need to maintain multiple copyright statements across projects.
Change Company Name within Xcode
In the Groups and Files window, right click on the project name and select Get Info:

In the General Settings tab, near the bottom you’ll see a field for the Organization Name:

Update this field and the new value will be reflected in each source code file you create in the active project.








