How can I load a local HTML file into the UIWebView?

前端 未结 7 898
有刺的猬
有刺的猬 2020-12-09 03:17

How can we load our own html file into the UIWebView?

相关标签:
7条回答
  • 2020-12-09 03:19

    The following code will load an HTML file named index.html in your project folder:

    [WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
    
    0 讨论(0)
  • 2020-12-09 03:21

    Swift

        guard let path = NSBundle.mainBundle().pathForResource("index", ofType: "html") else {
            return
        }
    
        let url = NSURL(fileURLWithPath: path)
        self.webview.loadRequest(NSURLRequest(URL:url))
    
    0 讨论(0)
  • 2020-12-09 03:23
    NSString *htmlFile = [[NSBundle mainBundle] pathForResource:@"privacy-Policy" ofType:@"html" inDirectory:nil];
    NSData *htmlData = [NSData dataWithContentsOfFile:htmlFile];
    [webView loadData:htmlData MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@""]];
    
    0 讨论(0)
  • 2020-12-09 03:29

    By using following code you can load an html in an WebView.here web is web view obj and inde

    Web.delegate = self;
    
    [Web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"]isDirectory:NO]]];
    
    0 讨论(0)
  • 2020-12-09 03:33

    Cody Gray is right but there's also this way :

    // 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]];
    

    This is useful if you need to edit the html before you load it.

    0 讨论(0)
  • 2020-12-09 03:35

    Swift Version 2.1

    // load html to String with Encoding
    let path = NSBundle.mainBundle().pathForResource("policy", ofType: "html")
    do {
        let fileHtml = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
        webView.loadHTMLString(fileHtml as String, baseURL: nil)
    }
    catch {
    
    }
    
    0 讨论(0)
提交回复
热议问题