如何将本地HTML文件加载到UIWebView中?

RAM*_*ANA 25 iphone uiwebview ios

我们如何将自己的html文件加载到UIWebView中?

Cod*_*ray 64

以下代码将加载index.html项目文件夹中指定的HTML文件:

[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
Run Code Online (Sandbox Code Playgroud)


dea*_*rne 28

科迪格雷是对的,但也有这样的方式:

// Load the html as a string from the file system
NSString *path = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];

// Tell the web view to load it
[WebView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
Run Code Online (Sandbox Code Playgroud)

如果您需要在加载之前编辑html,这非常有用.


Ode*_*gev 6

迅速

    guard let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") else {
        return
    }

    let url = NSURL(fileURLWithPath: path)
    self.webview.loadRequest(NSURLRequest(URL:url))
Run Code Online (Sandbox Code Playgroud)