【问题标题】:iOS UIWebview Load local device HTML file path [duplicate]iOS UIWebview加载本地设备HTML文件路径[重复]
【发布时间】:2013-09-14 00:05:35
【问题描述】:

我正在开发一个 iOS 应用程序,我想在其中加载 UIWebView 上的 HTML 文件,我的 html 文件路径如下所示,

iPhone HTML 文件路径:

/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html

我想将此 iPhone 设备的 HTML 文件路径加载到 UIWebView

我该怎么做?


非常感谢您的快速回答。让我解释一下我的基本要求, 我正在开发一个 iOS 应用程序,我的要求是在屏幕上填写一些字段并生成一个 .pdf 文件。

为此,我采用了一种预定义的 HTML 表单,然后我更新了该 .html 文件并通过以下代码保存了它:

NSString *fileName = @"new_file_htmlfile.html";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *html_file_name_location = [documentsDirectory stringByAppendingPathComponent:fileName];

NSError *error;

// Write the file
[data writeToFile:html_file_name_location atomically:YES
        encoding:NSUTF8StringEncoding error:&error];

通过这样做,我生成了新的 HTML 文件,并从代码中读取了文件路径。 iPhone设备路径为:

/var/mobile/Applications/65A8E000-21A6-429E-90A0-6BF03BAB3EA5/SarkBuilderApp.app/SarkBuilerHtmlFile_new.html

iPhone模拟器路径为:

/Users/rightwaysolution/Library/Application Support/iPhone Simulator/6.1/Applications/20748991-EE5C-44FE-ACFF-D93542FCF95B/Documents/new_file_htmlfile.html

现在我正在尝试在我的 UIWebView 上加载该 iPhone 设备 HTML 文件,以便将其转换为 .pdf。

能否请您解释一下如何访问该文件?

【问题讨论】:

  • 您要阅读的 HTML 文件需要驻留在您的应用程序沙箱中。请参阅上面的链接来做到这一点。如果没有越狱,就无法读取其他应用程序包中的 HTML 文件。

标签: iphone ios uiwebview


【解决方案1】:

使用这段代码来实现你想要的

NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"SarkBuilerHtmlFile_new" ofType:@"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

但为此请确保您的 html 文件必须位于 xcode 中的应用文件夹中

编辑:-

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

NSString* htmlString = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"txtFile.txt"];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

NSString* htmlString = [NSString stringWithContentsOfFile:content encoding:NSUTF8StringEncoding error:nil];
[webView loadHTMLString:htmlString baseURL:nil];

【讨论】:

  • 嘿,感谢您的回答,但很抱歉,因为我在我的问题中提供的 html 文件的路径不是来自 xcode 的文件夹。因为我已经创建了一个 html 文件,并且我已经将该文件保存在我的本地存储中,并且在保存该 html 文件后,我希望该文件显示在 uiwebview 上。该路径来自 iphone 内部存储或 iphone 模拟器内部存储。如果您给出从该 iphone 内部存储文件加载 html 文件的任何答案,请帮助我。
  • 有两种方法可以读取本地 html 文件,一种是您的 html 文件在您的应用程序中并使用 xcode,另一种是您的 html 文件位于应用程序的文档目录中......所以如果在这两种情况下你需要任何帮助,我可以帮助你。
  • 嘿 eptdeveloper ,我已经更新了我的问题,以便您了解我的要求,您能帮帮我吗?
  • 通过这种方式,您可以找到存储在documents目录中的文件,然后使用它在webview上打开它stackoverflow.com/a/6546616/2695503
  • 嘿 eptdeveloper,非常感谢您的回答,现在我从该文档目录路径读取文件,现在我的新问题是,现在我已经阅读了我创建的 html 文件,如何我可以以编程方式在 UIWebView 上加载该文件吗?你能帮我用什么代码加载那个文件路径吗?我的 html 文件路径是 iphone 路径:/var/mobile/Applications/FEE7BD45-5714-4F26-A527-A1FBF2871644/Documents/filename.html
猜你喜欢
  • 1970-01-01
  • 2017-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-02
相关资源
最近更新 更多