我使用以下代码行保存了一个 html 页面:
NSURL *yoyoyo = [[NSURL alloc] initWithString:@"www.google.com"];
NSData *data = [[NSData alloc] initWithContentsOfURL:yoyoyo];
NSString *html = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSString *documentsDirectory = @"/Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/";
NSLog(@"%@", documentsDirectory);
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:@"file.html"];
[html writeToFile:htmlFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
名为 as 的文件被保存为提到的路径中的“file.html”文件夹,即 documentsDirectory。有没有一种方法可以在我的应用程序的 safari 或 webView 中打开此文件?感谢和问候。
最佳答案
要在您的 WebView 中加载它,请执行以下操作
NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:@"file.html"];
NSString *htmlContents = [NSString stringWithContentsOfFile:htmlFilePath
encoding:NSUTF8StringEncoding
error:NULL];
[webView loadHTMLString:htmlContents baseURL:nil];
您在获取 documentDir
时也遇到了问题
代替
NSString *documentsDirectory = @"/Users/xxxx/Library/Application Support/iPhone Simulator/5.1/Applications/";
使用
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
再次保存文件并尝试使用上面的代码加载
关于iphone - 在 WebView 或浏览器中启动存储的 html 页面 - Objective C/Xcode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11081634/