Cocoa
iPhone Developer Tips Stats:
|
Get Application Icon Name
In a previous post, Get Application Name, I wrote a line of code to get the application name from the app bundle.
The line of code below is a slight modification that shows how to get the name that will appear on the iPhone below the icon:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
The CFBundleDisplayName value corresponds to the Bundle [...]
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 [...]
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");
Download, Create and Display an Image from URL
This tip will show the steps to download and display an image from a remote resource. This is handy if you need to add an image as a subview, yet, the image is not part of your application bundle.
URL to Remote Image
We start by creating a URL to the remote resource:
NSURL *url = [NSURL [...]
Date Formatter Examples – Take 4: Setting Locale
A reader recently wrote and asked how to show a date output in a different language. By default, the NSDateFormatter will use the locale set on the device, so no code specific locale changes are required if you want your app to display in the current locale.
However, if you need to display a date [...]
iPhone SDK 3 and Deprecated Method Warnings
A side-effect of developing code on platforms that continue to evolve, is dealing with methods that become deprecated from one release to another. If you’ve spent anytime at all with Java, you are all too familiar with this concept. With the release of the iPhone SDK version 3.0, a number of methods have been become [...]
UIColor Macros
Below are two macros I paste inside every new iPhone project. Besides saving a few keystrokes, they work well when using the color picker application. Let’s look at the macros first:
#define RGB(r, g, b)
[UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
#define RGBA(r, g, b, a)
[UIColor colorWithRed:r/255.0 [...]
Changing Views to Landscape Mode
As you well know, by default, iPhone applications launch in portrait mode. If you need to start your application in landscape mode, you can set UIInterfaceOrientation key in the plist file. The two possible values for this key are: UIInterfaceOrientationLandscapeLeft (iPhone home button will be on the left) and UIInterfaceOrientationLandscapeRight (home button on right).
Introduction to Protocols
What follows is a quick introduction to working with protocols. This is good background information to understand as protocols are common in various Cocoa frameworks. A protocol is means to define a list of required and/or optional methods that a class implements. If a class adopts a protocol, it must implement all required methods in [...]
Basics of Notifications
What follows is a brief guide to working with Notifications in Cocoa. I’ll cover the basics, including registering an observer and posting notifications, just enough to start using notifications in your iPhone apps.
There is an instance of NSNotificationCenter available to every running iPhone application. This class acts as an intermediary to facilitate communication between [...]
Single, Double and Triple Taps
If you need to tinker with the threshold (time between clicks) when working with single, double and triple taps on the touch screen, one approach for this follows. What follows is a short example that demonstrates how you can manage the delay between taps.
Get Application Name
If you ever need to get the name of your application in code, for example, to display the application name across a navigation bar, it’s as near as the bundle for your application.
The code to get the name is as simple as this:
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
The breakdown is as follows:
NSNumber and NSInteger
If you’ve ever found yourself scratching your head thinking “now which one should I be using, NSNumber or NSInteger?” the short summary below should help.
NSInteger is nothing more than a synonym for an integer. What follows is how NSInteger is defined:
Read and Write User Preferences
Reading and writing user preferences within iPhone applications is surprisingly easy given the NSUserDefaults class does most all the work for you.
What follows is a short example to show how you can read/write two values, a boolean and an integer. The example assumes you want to save state as to whether a user wants [...]
Launching Your Own Application via a Custom URL Scheme
One of the coolest features of the iPhone SDK is an application’s ability to “bind” itself to a custom URL scheme and for that scheme to be used to launch itself from either a browser or from another application on the iPhone. Creating this kind of binding is so simple, its almost criminal not [...]








