Sleep, Pause or Block a Thread
If you ever need to briefly pause a thread of execution, there are two class methods in NSThread you can use. Although you have to use these methods judiciously, they can be handy in instances where you need to force a delay in order to achieve some desired effect.
For example, if you want to slowly fade-in (increase) the volume of the audio player over a few seconds, you could write a loop to bump the volume a small increment each time through the loop, and pause briefly before heading back to the top of the loop. If you didn’t include a short pause within the loop, the application may run the loop so quickly that you could not audibly detect the fade-in.
sleepForTimeInterval
One approach is to use the class method sleepForTimeInterval method:
+ (void)sleepForTimeInterval:(NSTimeInterval)ti
The variable NSTimeInterval is of type double and represents the number of seconds to sleep
// Block for .5 seconds [NSThread sleepForTimeInterval:.5];
sleepUntilDate
Another approach is to use an NSDate object to specify a specific time in the future to resume the thread:
+ (void)sleepUntilDate:(NSDate *)aDate
With this method you pass in an NSDate object that specifies how long to block the thread.
// Block for 2.5 seconds from "now" NSThread sleepUntilDate: [NSDate dateWithTimeIntervalSinceNow:0.5]];
Rotate an Image or Button with Animation – Part 2
In a previous post Rotate an Image with Animation – Part 1 I wrote a short code block using CGAffineTransform to rotate an image. Problem is, if you want to rotate an image 360 degrees, or repeat a rotation a specified number of times, you’ll need to take a different approach, which is the focus of this post. This code example will use a layer for animation so this same effect can used on images, buttons, etc.
The video below shows a rotating a button (on the left) as well as image. Notice the direction of rotation is clockwise on the button and counter-clockwise on the image. Also, notice that the rate of the spin is different for each object.
Continue reading this post »
Resize/Scale of an Image – Take 2 – Coding a Thread Safe Approach
In the first post on image resizing, How to Resize/Scale an Image using an Objective-C Category, I wrote barebones approach to resizing an image. This works well for simple cases, however this approach is not thread safe as it uses the global current context.
I attended a recent Apple Tech Talk and one of the more interesting discussions was on how to create code to dynamically resize images, using an approach that is thread safe. An experienced developer at the event was willing to share an excellent code example that I’ll walk through in this post. The code is a fair amount more complex than the first version I wrote, however, with the complexity comes flexibility.
Continue reading this post »
Create Readable Links to Your iPhone Apps and Company in the App Store
Creating a link to your application or company in the App Store serves many purposes, linking from your website, sending a link to a friend or app review website, etc. You can easily create a link to your application by right clicking on the application name or icon in iTunes. For example, referring to Cartoons to Go application, if you right-click on the application name you end up with this link: http://itunes.apple.com/us/app/cartoons-to-go/id305713868?mt=8.
Continue reading this post »
Sam Kinison and Scream Scenes iPhone Apps
Mill Creek Entertainment is in the business of providing video home entertainment. Their DVD product line consists of classic movies, television episodes, historical documentaries and feature films.
I’ve been working with Mill Creek to come up with creative ways to leverage their content in digital formats beyond DVD with the iPhone as the first mobile platform.
Scream Scenes was released in the fall of 2009 and featured audio and video clips from 16 of the most frighteningly fantastic classic horror films which can be found in Mill Creek Entertainment’s Horror Classics 50 Movie MegaPack.
If you tap on the any of the 16 movie icons, a short audio clip of a “classic scream” will play. If you touch and hold one of the icons, the video segment from the movie showing the “classic scream” will play.
Sam Kinison
Our second collaborative project was the Sam Kinison Scream Board, which follows the same idea of Scream Scenes, audio and video content from a collection of Sam Kinison DVDs. If you are not familiar with Sam Kinison, let’s just say his sense of humor can be a little shocking. If you are okay with Sam’s no holds barred approach (the app has a 17+ rating) his standup comedy makes for some great laughs.
The Sam Kinison app is free, if you are up for some outrageous fun, give it a try.
C Conditional Operator aka Ternary Operator
I’ve always been fond of the conditional operator in C, which is essentially a terse way to write an if/else statement. Given this operator works with three operands, it is often fondly referred to as the ternary operator.
The conditional looks as follow:
condition-test ? first-expression : second-expression
If the condition-test is nonzero, the first-expression is evaluated, otherwise the second-expression is evaluated.
Continue reading this post »
Play iPhone Movies in Portrait Mode with MPMoviePlayerController using Public API’s
It’s been covered by a number of websites and blogs on how to play movies in portrait mode using MPMoviePlayerController. Problem is, every solution that I’ve been able to find uses private API’s to tell the player to flip the direction of play.
Other than the built-in movie player, another option is to use a UIWebview, however, there are a few drawbacks including no support for notifications on when a movie has been preloaded, which is handy for displaying a “please wait” loading message.
This post is introduces another approach to playing a movie in portrait mode without delving into private API’s, and I think you’ll agree, it’s quite clever.
Continue reading this post »
Own an iPhone or iPod touch? I Could Use Your Help To Test a Streaming Video App
Cartoons to Go is a streaming video application that plays vintage cartoons (from 1920’s through 1950’s). Recently there have been a handful of comments about the application not working as expected.
I am looking for 10-15 people who own an iPhone or iPod touch to give the application a try. I’ll create a build specifically for your device or provide a promo code to download the latest version.
What I ask for in return is a thorough run through of the app, testing the streaming speed and performance, as well as exercising the overall UI.
Please send me an email if you are interested to help out jwmuchow at gmail dot com
Accessing iTunes Reports When iTunes is Closed
Over the holiday Apple shutdown iTunes:

Even though you can’t use iTunes to upload new apps, change current app descriptions, prices, etc, you can access your reports with the following link: https://itts.apple.com
NSRange and NSString Objects
When poking around NSString methods you’ll find many references to NSRange, which is nothing more than a C structure that is helpful for describing a series of items, including a starting location and a count. For example, a range is helpful to extract a substring from another string, where you specify the starting location and number of elements needed (examples to follow).
NSRange Definition
NSRange is a structure defined as follows:
typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange;
Cartoons to Go! iPhone Application – Version 1.2
Back in mid-November I posted about a personal iPhone project, Cartoons to Go!. In early December I submitted the 1.1 release which included a major overhaul to the UI going with a retro-TV interface as shown in the figure below:
Compare NSString Objects (Updated)
This tip is for those new to Objective-C and Cocoa and walks through some basics on comparing NSString objects for equality.
Compare NSString objects with ==
NSString *str1 = @"Homebrew"; NSString *str2 = @"Homebrew"; // This compares the addresses of the string if (str1 == str2) NSLog (@"str1 equals str2"); else NSLog (@"str1 does not equal str2");
Show Your Support for iPhoneDeveloperTips.com
Since launching iPhone Developer Tips back in August of 2008, there have been almost 200 tips, tricks and tutorials posted, an average of just over 12 posts per month. The number of page views on a monthly basis is pushing 70,000. All things considered, not too bad…
My primary motivation with this blog is to give back to the same community of developers who have provided help and insight to me over my 20 years in software development. Beyond the obvious time commitment and labor of love to keep writing content, there are costs to keep this machine running – if you would like to show your support for iPhone Developer Tips, please consider clicking through the Amazon link below as you go about your holiday shopping, or if you prefer, a Paypal donation is welcome:
![]()
Thank you very much and Happy Holidays!
John
Get iPhone Device Name, Unique Device Identifier (UDID), OS and Model
The iPhone SDK includes a singleton of class type UIDevice that you can use to retrieve device specific information (this works equally as well with the simulator).
Available Properties in UIDevice
- uniqueIdentifier – identifier guaranteed to be unique for every device
- name – arbitrary name found in General > About setting on device
- systemName – name of the OS running on the device
- systemVersion – current version of the OS
- model- model, such as ”iPhone” or ”iPod touch”
- localizedModel – same as above using a localized string
Tell Xcode Not to Compile a File
By default, when you create a new file or import a file into Xcode, if the file type is recognized as a source file (.c, .m .js, etc), the file is added to the Compile Sources folder in the application target.
You may not always prefer this default action – for example, if a JavaScript file is imported, Xcode does not know what to do with this file type so a compile warning is generated, see the example below:

I ran into this recently when I added a JavaScript file that is associated with an Openx ad that I wanted to displayed inside my application, using a UIWebView.
There is a simple fix, drag/drop the offending file(s) from the Compile Sources folder to the Copy Bundle Resources folder within the applications Target, which will simply include the file in the application bundle with no processing of the file (this is where you will typically see image files, sound files, etc).
In the image below, the file foo.js has been moved into the Copy Bundle Resources folder:











