Cocoa
Obfuscate / Encrypt a String (NSString)
Apple Keychain Services offer a secure means to store sensitive information. Through the keychain, all the hardwork is managed for you to store and retrieve content. As powerful as the keychain services are, I was recently tinkering with some code to see if I could obfuscate content within an application. I had a few strings [...]
Truncate a String and Append an Ellipsis, Respecting the Font Size
A number of the UI related controls will automatically truncate and append ellipsis with no effort required on your part. For example, with UILabel you can specify the linebreak mode to indicate how you would like the system to manage wrapping and truncating the label text. However, they are times when having a method to [...]
NSNotification, UserInfo and Object Methods
When working with an NSNotification object, you’ll want to familiarize yourself the userInfo dictionary, which provides access to any additional objects that may be of interest to the receiver. Understanding the object method may also be helpful if you are using the same notification on more than one object.
Add Rounded Corners and Border to UIWebview
Beyond simply displaying websites, UIWebview can be a nice alternative when you need to display formatted text, not to mention the option of incorporating JavaScript which presents some very interesting scripting and UI opportunities. I’ve found webviews handy for presenting help information, displaying HTML downloaded from remote servers as well as integrating custom ad banners [...]
Formatting Numbers – NSNumberFormatter Examples
The previous post on how to Get Total and Free Space on the Mounted File System is a good segue to this post, as the formatting of the output from the previous code example left a little to be desired, see the figure below. Without delimiters to mark off thousands, it’s a little difficult to [...]
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 [...]
Storing CGPoint, CGSize and CGRect in Collections with NSValue
In an earlier post CGRect, CGSize and CGPoint Functions I demonstrated a number of geometry structures available for representing a point (CGPoint – x and y coordinates), size (CGSize – height and width) and rectangles (CGRect – combination of both). Unfortunately, you cannot directly store any of the above in a collection, for example an [...]
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 [...]
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 [...]
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 [...]
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 green:g/255.0 blue:b/255.0 alpha:a] Here’s the code without/with the [...]
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).








