html - 你将如何在 iOS 上的 UIWebView 中创建一个新的本地 HTML 文件?

标签 html ios uiwebview local

首先,我想指出,我是网络开发的新手,对 iOS 开发更熟悉,所以如果有一些我不理解的基本知识,请原谅。

我已经了解了如何将 HTML 文件放入应用程序目录并将其加载到 Web View 中 (Example)。这很好,但是如何在应用程序中创建新的本地 HTML 文件?这样用户就可以创建一个新的 html 文件来输入,然后存储它(基本文档样式应用程序功能)。也许使用某种 Javascript(我对这种 Javascript 不太熟悉)?

最佳答案

您可以像这样在 NSString 中构建 HTML:

// get user input

NSString *userText = @"Hello, world!";

// build the HTML

NSString *html = [NSString stringWithFormat:@"<html><body>%@</body><html>", userText];

// build the path where you're going to save the HTML

NSString *docsFolder = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filename = [docsFolder stringByAppendingPathComponent:@"sample.html"];

// save the NSString that contains the HTML to a file

NSError *error;
[html writeToFile:filename atomically:NO encoding:NSUTF8StringEncoding error:&error];

很明显,我只是将 userText 设置为一些文字,但您显然可以让用户将其键入 UITextView 并从那里获取它。

然后您可以将 HTML 加载到 Web View 中:

[self.webView loadHTMLString:html baseURL:nil];

或与:

NSURL *url = [NSURL fileURLWithPath:filename];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];

关于html - 你将如何在 iOS 上的 UIWebView 中创建一个新的本地 HTML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15561410/

相关文章:

ios - 来自 Google 的 Apple Maps 转换

ios - 如何禁用缩放并保留对 UIImagePickerController 的控制?

iOS 8 JavaScript 到 UIWebView 的通信错误

ios - 使用 RxSwift 在 UIWebView 中捕获点击的链接

iphone - iOS-UIWebview-3G剥离评论

javascript - 使用递增的列数创建 html 表

html - 表格单元格在浏览器调整大小时调整大小

ios - ContainerView 位置不正确

javascript - 脚本正确,但无法在浏览器中运行

javascript - Uncaught ReferenceError : function is not defined