0

我正在尝试从 Internet 下载 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);
4

1 回答 1

-1

因为你不应该做这些stringByAddingPercentEscapesUsingEncoding:事情,这会将 URL 变成无效的http%3a%2f%2f......

直接使用

NSData *theData = [NSData dataWithContentsOfURL:
    [NSURL URLWithString:@"http://subdomain.somesite.com/profile.html"]];
于 2010-01-31T07:21:43.870 回答