Xcode UIWebView 本地 HTML
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15654509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode UIWebView local HTML
提问by eds1999
I know HTML, javascript, CSS… but I wanted to make a native/hybrid iPhone app using HTML5 but without using something like PhoneGap or Nimblekit. I never wrote a real (not web app) iPhone app before, so i don't really know anything about Xcode. I already made a UIWebView with a tutorial that i found, but it displays a website (apple.com). How can i make this display a local file (index.html)?
我知道 HTML、javascript、CSS……但我想使用 HTML5 制作原生/混合 iPhone 应用程序,但不使用 PhoneGap 或 Nimblekit 之类的东西。我以前从未写过真正的(不是网络应用程序)iPhone 应用程序,所以我对 Xcode 一无所知。我已经用我找到的教程制作了一个 UIWebView,但它显示了一个网站 (apple.com)。我怎样才能让它显示一个本地文件(index.html)?
My code in ViewController.m under (void)viewDidLoad:
我在 ViewController.m 中的 (void)viewDidLoad 下的代码:
[super viewDidLoad];
NSString *fullURL = @"html/index.html";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_viewWeb loadRequest:requestObj];
回答by Eli Ganem
You have 2 options:
您有 2 个选择:
Insert HTML directly like this:
像这样直接插入 HTML:
UIWebView *wv = [[UIWebView alloc] init];
[wv loadHTMLString:@"<html><body>YOUR-TEXT-HERE</body></html>" baseURL:nil];
Load an html file that exists in your project:
加载项目中存在的 html 文件:
UIWebView *wv = [[UIWebView alloc] init];
NSURL *htmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"] isDirectory:NO];
[wv loadRequest:[NSURLRequest requestWithURL:htmlFile]];
If you want your example to work, then replace the code you sent with this:
如果你想让你的例子工作,那么用这个替换你发送的代码:
[super viewDidLoad];
NSURL *htmlFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]];
[_viewWeb loadRequest:[NSURLRequest requestWithURL:htmlFile]];
回答by Ramtin
For Swift 3:
对于 Swift 3:
@IBOutlet var webView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
let URL = Bundle.main.url(forResource: "file name", withExtension: "html")
let request = NSURLRequest(url: URL! as URL)
webView.loadRequest(request as URLRequest)
}
回答by Dipen Panchasara
you can load local html file from your project bundle as following
您可以从项目包中加载本地 html 文件,如下所示
NSURLRequest *requestObj = [NSURLRequest requestWithURL:[[NSBundle mainBundle] URLForResource:@"index" withExtension:@"html"]];
[myweb loadRequest:requestObj];