我正在尝试从互联网下载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);
答案 0 :(得分:-1)
因为您不应该执行stringByAddingPercentEscapesUsingEncoding:
内容,这会将网址变为无效的http%3a%2f%2f
....
直接使用
NSData *theData = [NSData dataWithContentsOfURL:
[NSURL URLWithString:@"http://subdomain.somesite.com/profile.html"]];