Как скачать файл .html для локального использования? - PullRequest
0 голосов
/ 31 января 2010

Я пытаюсь загрузить файл HTML из Интернета и сохранить его в своем приложении для последующего (автономного) просмотра. Я использую следующий метод для загрузки файла, но мой отчет NSLogs об отсутствии данных был загружен. В чем здесь проблема? (предположить активное подключение к Интернету, предположить заранее определенный путь к локальному каталогу документов)

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);

1 Ответ

0 голосов
/ 31 января 2010

Потому что вы не должны делать вещи stringByAddingPercentEscapesUsingEncoding:, которые превращают URL в недействительный http%3a%2f%2f ....

Прямое использование

NSData *theData = [NSData dataWithContentsOfURL:
    [NSURL URLWithString:@"http://subdomain.somesite.com/profile.html"]];
...