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