HTTP Basic Authentication – Accessing Password Protected Servers
It’s not uncommon for a server to require credentials for access. A common example is a password protected directory on a web-server that will prompt for a username and password before allowing access.
When using a NSURLRequest object to access a server, the delegate methods of NSURLConnection offer a means to be notified when an authentication challenge has been requested.
This post will show one approach for managing URL requests that require authentication.
Initiate the URL Request
/*--------------------------------------------------------------------------- * Begin the authentication process *--------------------------------------------------------------------------*/ - (void)startAuthentication { NSURL *url = [NSURL URLWithString:@"http://protectedURL.com"]; NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url]; // Start the connection request // Assumes NSURLConnection *urlConnection is defined as an // instance variable urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self]; }
Answer Authentication Challenge
If an authentication challenge is returned from the URL request, the method below will be called. To respond to the challange, create a NSURLCredential object, setting the username and password.
Notice that we can check to see how many times the challenge/response has failed and present an error message as needed.
/*--------------------------------------------------------------------------- * Received a server challenge *--------------------------------------------------------------------------*/ - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { // Access has failed two times... if ([challenge previousFailureCount] > 1) { [urlConnection release]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Authentication Error" message:"Too many unsuccessul login attempts." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alert show]; [alert release]; } else { // Answer the challenge NSURLCredential *cred = [[[NSURLCredential alloc] initWithUser:@"admin" password:@"password" persistence:NSURLCredentialPersistenceForSession] autorelease]; [[challenge sender] useCredential:cred forAuthenticationChallenge:challenge]; } } /*--------------------------------------------------------------------------- * Finished loading URL *--------------------------------------------------------------------------*/ - (void)connectionDidFinishLoading:(NSURLConnection *)connection { NSLog(@"Connection success."); [urlConnection release]; } /*--------------------------------------------------------------------------- * URL connection fail *--------------------------------------------------------------------------*/ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"Connection failure."); [urlConnection release]; }
iOS Open Source : Heads Up Display with MBProgressHUD
When the iPhone SDK was released, just over 2 years ago, open source projects specifically targeting the iPhone OS were few and far between. Over the past year there has been a significant uptick in iPhone/iOS open source projects, and with that, I’d like to share various projects as I come across them. Should a good Cocoa library cross my radar, one that is applicable to iOS, I’ll pass that on as well.
Continue reading this post »
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 defined as constants and I was interested to see if there was a painless way to store the values as obfuscated strings, and when running the application, un-obfuscate the strings and use the same within the application.
Continue reading this post »
Voices That Matter iPhone Developers Conference – Philadelphia PA – October 16th and 17th, 2010
Pearson Education just announced the Voices That Matter iPhone Developers Conference to be held October 16-17, 2010 in Philadelphia.
As with all previous Voices That Matters iPhone events, you will have an opportunity to learn from industry leaders who literally “Wrote the Books” on iOS Development. This two-day conference features a wide range of sessions, including:
* Multitasking
* Game design
* Accessibility
* Designing a killer user interface
* Augmented reality
* Unit testing
* Internationalization
* Many other timely and compelling discussions
Lead by industry experts and authors, this is a weekend packed with sessions and interactive discussions,
Enjoy a unique venue that offers an opportunity to network with other developers and mingle with experts including Mike Lee, Aaron Hillegass, Erica Sadun, Stephen Kochan, Rod Strougo, and Christian Hofstader, to name a few.
Register Early for the Best Prices!
Register before September 10th and the price is only $495. Provide priority code PHEM232 when registering to take an additional $100 off and attend for just $395!
Capture iPhone Simulator Screenshots – Revisited – iPhone Simulator Cropper
A colleague on a recent iPhone project was looking for a tool to capture a series of screenshots for an upcoming application demo. In a Google search he first bumped into a post I wrote back in February of 2009: Capture iPhone Simulator Screenshots – Open Source Screen Capture Tool, a nice Mac tool for taking screenshots. As luck would have it, he also found another screen capture tool, one focused solely on iPhone screenshots: iPhone-Simulator Cropper.
Continue reading this post »
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 truncate a string without using a UI control would be handy. In this post I’ll create a category which will add a method to the NSString class to do just that. You can read more about working with categories in this post: Introduction to Categories.
Continue reading this post »
Download and Install Older Versions of Xcode (Xcode Previous Releases)
As a general rule of thumb, it’s best to stick with the latest release of Xcode. That said, there are times when running an older release can be a good thing.
You can download older versions of Xcode by logging into Apple Developer Connection – Downloads and ADC Program Assests (http://connect.apple.com) – You will need to be a member of the developer program to login.
Continue reading this post »
How to Play Movies from the Application Bundle or from a Remote Server (URL)
In the previous two posts I wrote about how to create a movie player that would work across OS versions, as well as how to play a movie in portrait mode (on 3.2 and above) : Display iPhone Movies in Portrait Mode and Getting MPMoviePlayerController to Cooperate with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK
For both posts, in order to create a working example that one could download and run as is, the code examples referenced a movie that I included in the application bundle. Although handy, this prompted a number of questions about how to play a movie from a remote resource.
I’ve updated the original examples for both posts, with an option to play movies from the application bundle or from a remote server.
Continue reading this post »
Display iPhone Movies in Portrait Mode (Updated)
In the previous post, Getting MPMoviePlayerController to Cooperate with iOS4 and Earlier Versions of iPhone SDK, I demonstrated how to work with the most recent changes in iPhone OS 3.2 and 4.0 MPMoviePlayerController to enable one application to play movies regardless of the OS version on the device.
In this post I want to show you a few minor changes you can make to the previous app to display a movie in portrait mode, something that was not possible in earlier (pre 3.2) OS versions. With that said, back in January of 2010 I did cover a means to create the illusion that a movie was in portrait mode, if this piques your interest, have a look here: Play iPhone Movies in Portrait Mode with MPMoviePlayerController using Public API’s
Continue reading this post »
Getting MPMoviePlayerController to Work with iOS4, 3.2 (iPad) and Earlier Versions of iPhone SDK
The API and overall approach for working with MPMoviePlayerController has changed just enough in the 3.2 SDK (iPad) and iOS4 SDK to cause working applications in earlier releases to be problematic when running on later SDKs. In this post I’ll walk through a short example of a movie player application that will work with 3.x, 3.2 and 4.0 SDKs.
In the next post, Display iPhone Movies in Portrait Mode, I’ll show how to create a movie player that runs in a view that is not fullscreen as well as how to show a few lines of code to display a movie in portrait mode – the one caveat here is that both of the tips in the second post will apply only to OS versions 3.2 and up.
Continue reading this post »
Developing iPhone Apps with iOS4 SDK, Deploying to 3.x Devices : Base SDK and iPhone OS Deployment Target
Editor’s Note:You can download and install any previous release of Xcode if you would like to roll back to an earlier version. You can get the specifics here: Download and Install Older Versions of Xcode.
If you’ve installed Xcode 3.2.3 you quickly became aware that the only SDK’s packaged with this version are 4.0 and 3.2. Until 4.x has widespread adoption, chances are you’ll want your applications to run on earlier versions of the iPhone OS (iOS) SDK. You can accomplish this feat through two configuration options within Xcode, the Base SDK and the iPhone OS Deployment Target.
Continue reading this post »
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.
Continue reading this post »
Announcing iPhone/iPad DevCon 2010 : Sept 27th through 29th in San Diego, California
BZ Media, publisher of SD Times, is proud to announce the iPhone/iPad DevCon being held September 27th – 29th, 2010 in sunny San Diego, California.
iPhone DevCon has over 45 sessions to choose from, covering everything from design and development to marketing of iPhone and iPad applications.
Beyond the comprehensive selection of workshops and classes, you won’t want to miss the keynote presentations from two highly regarded Cocoa/iPhone developers: Aaron Hillegass, CEO of Big Nerd Ranch and author of the best-selling book Cocoa Programming, and Mike Lee, cofounder of Tapulous, creators of Tap Tap Revenge.
If you register by July 30th you can save $450 over the full price registration!
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 from open source ad servers such as Openx.
This tip shows how to add rounded corners and a colored border to a UIWebview, which adds a nice touch to an otherwise squared-off look.
Continue reading this post »
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 read. Welcome NSNumberFormatter, which provides a simple means to comma delimit NSNumber objects.
Continue reading this post »









