Feb 03

To download a small amount of data from a URL it’s very convenient to use:

NSData *downloadData = [NSData dataWithContentsOfURL:url];

To get data from a URL that requires authentication there several classes that specifically deal with this, e.g. NSURLAuthenticationChallengeSender and NSURLCredential, which require that you use NSURLConnection instead. 

NSURLConnection has several other advantages such as asynchronous downloading, but if you just want a one-liner, you can still use NSData dataWithContentsOfURL with basic authentication using the following method.

Normally the URL that you pass to dataWithContentsOfURL looks something like https://www.mysite.com/getmydata

You can add a username and password directly in the URL like this https://username:password@www.mysite.com/getmydata and this type of URL works just fine with NSData dataWithContentsOfURL.

Security Implications

  • When you include the username and password in the URL, they may be stored in the web server’s log file.
  • If you don’t use SSL, the username and password are sent in clear text.
  • Don’t store the URL including the username and password in a property file or plist. These files can easily be viewed by someone looking inside your app bundle.
  • Don’t store the credentials like this: NSString *credentials = @”user:password@”; This string is very easy to find in the executable file. If your security requirements are low then you can apply some mild obfuscation: NSString *credentials = [NSString stringWithFormat:@”%c%s%@%c%c%s%@”, ‘u’, “ser:”, @”pas”, ‘s’, ‘w’, “ord”, @”@”]; If you have real security requirements, use real encryption.


written by Nick \\ tags:

Oct 02

One of the great advantages of the iPhone platform is that it’s a single target to develop for. It doesn’t suffer from the “write once, test everywhere” syndrome of J2ME. But with 2.0, 2.1 and future firmware releases, not to mention rumored tablets and other new touch products, this will change.

The other day I managed to write an application that worked on 2.0 but not 2.1 devices. After some investigation I found that the root cause was a subtle change Apple made in 2.1 without any notification in release notes or in the SDK documentation.

Consider this very common snippet of code:

NSData *webData = [NSData dataWithContentsOfURL:url];

If the url in the above example refers to a file that is gzipped, e.g. www.myserver.com/file.gz, NSData will now (in 2.1) unzip the data automagically. So if your code was expecting to receive a representation of the compressed file, it will now break.

Most browsers will to the gzip/gunzip automagically when a web server sends compressed content. This is done to save bandwidth, improve download speed, and is generally a Good Thing. When the same “feature” is implemented in the SDK and you are not given any control over the behavior, that’s a Bad Thing.

The moral of this story is that you need to test all your code on both 2.0 and 2.1 devices.

written by Nick \\ tags:

Apr 07

First you need to add your file to the Resources folder of your Xcode project. Then you can access the file like this (assuming the file is called MyFile.txt):

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
if (myData) {
	// do something useful

Here’s a complete example reading a help text file into a UIWebView.

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"HelpDoc" ofType:@"htm"];
NSData *htmlData = [NSData dataWithContentsOfFile:filePath];
if (htmlData) {
	[webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"http://iphoneincubator.com"]];

If you want to read the file into a string, which you can then display in a UITextView, for example, then do this:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"important" ofType:@"txt"];
if (filePath) {
	NSString *myText = [NSString stringWithContentsOfFile:filePath];
	if (myText) {
		textView.text= myText;

written by Nick \\ tags: , , ,