Basic Authentication with a NSURLRequest in Cocoa Touch - While working on a certain unnamed iPhone app lately I ran across the need to use basic authentication in communication with REST services. For something that seems to be such a fundamental need for mobile applications I figured most of the work would be done for me. Turns out that's not the case. A few details are left up to you, the Cocoa Touch programmer. What Basic Authentication is In order to implement basic authentication in Cocoa Touch it is important to understand how it works. Basic authentication tokens are essentially formatted into a string in the format: username:password They are then Base64-encoded and formatted into an "Authorization" HTTP header who's value looks like: Basic c2NvdHQ6dGlnZXI= where "c2NvdHQ6dGlnZXI=" is the encoded token pair and "Basic" is a hard-coded prependage. Easy enough...
Asynchronous Programming in Cocoa Touch - It's unavoidable. Pretty much anywhere you go as a programmer these days concurrency is important. It's no less important on mobile platforms like the iPhone. In this post I intend to outline the fundamental techniques necessary for asynchronous programming on the iPhone. While these techniques could very well apply to standard, desktop Cocoa I'll focus on Cocoa touch. I'll cover three basic approaches: NSObject's performSelectorInBackground message, NSThread and NSTimer. NSObject's performSelectorInBackground message NSObject's performSelectorInBackground message can be used to easily farm off tasks that run asynchronously. Here's an example that could be placed in a view controller. -(void) workerThread { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSString *str = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.chrisumbel.com"]]; [pool release]; } - (void)viewDidLoad { [super viewDidLoad]; [self performSelectorInBackground:@selector(workerThread) withObject:nil]; } performSelectorInBackground simply executed workerThread asynchronously. Not much...











