iphone - 在 WebView 或浏览器中启动存储的 html 页面 - Objective C/Xcode

标签 iphone objective-c ios xcode ipad

我使用以下代码行保存了一个 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/

相关文章:

ios - 快速布洛克斯。从头开始指南

iphone - 在 Objective C 中序列化对象

iphone - 如何在 iOS 设备上编译 ARM 汇编文件和 iOS 模拟器使用常规 C 之间进行选择

objective-c - @property Objective-C

ios - 如何在用户滑动但不足以显示其他单元格时将 Collection View 单元格居中

ios - 使用 Xcode 中另一个场景中的按钮暂停音乐

iphone - 帮助需要遍历大的 JSON 提要

iphone - 如何在 NSUserDefaults 中存储 NSMutableArray

ios - 尝试圆形图像,输出菱形?

ios - 按下 UIButton 时运行函数