views:

104

answers:

1

I am trying to download an HTML file from the internet and store it in my app for later (offline) viewing. I use the following method to download the file but my NSLogs report that no data has been downloaded. Whats the problem here? (assume an active internet connection, assume a pre-defined path to local documents directory)

urlAddress = @"http://subdomain.somesite.com/profile.html";


            // Create and escape the URL for the fetch
            NSString *URLString = [NSString stringWithFormat:@"%@%@", @"ttp://subdomain.somesite.com/profile.html"];
            NSURL *URL = [NSURL URLWithString:
                          [URLString stringByAddingPercentEscapesUsingEncoding:
                           NSASCIIStringEncoding]];

            // Do the fetch - blocks!
            NSData *theData = [NSData dataWithContentsOfURL:URL];
            if(theData == nil) {

                NSLog(@"NO DATA DOWNLOADED");
            }

            // Do the write
            NSString *filePath = [[self documentsDirectory] stringByAppendingPathComponent:@"Profile/profile.html"];
            [theData writeToFile:filePath atomically:YES];

            NSLog(@"WRITING profile.html to path %@!", filePath);
A: 

Because you shouldn't do the stringByAddingPercentEscapesUsingEncoding: stuff, which turn the URL into an invalid one http%3a%2f%2f....

Directly use

NSData *theData = [NSData dataWithContentsOfURL:@"http://subdomain.somesite.com/profile.html"];
KennyTM
This is incorrect if you have characters in your parameter section of the URL. You will need to call stringByAddingPercentEscapesUsingEncoding for that portion of the URL, but not the http:// prefix.
Kevin Elliott