How to Load large HTML string in WebView with LoadHTMLString method?

前端 未结 2 1849
忘掉有多难
忘掉有多难 2021-01-24 06:41

I have to load an html string onto the UIWebView with \'LoadHTMLString\' method of UIWebView.

Please find the below code.

[wv loadHTMLString:[NSString st         


        
2条回答
  •  离开以前
    2021-01-24 07:20

    You just need to create one html file and put all html content in it, And use code to load that file

    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"] isDirectory:NO]]];
    

    And using this also

    NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
    NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    [self.webView loadHTMLString:html baseURL:[[NSBundle mainBundle] bundleURL]];
    

    Edit:

    I was overlooked that you have mention that loading file is working perfect.

    I think issue is because of you load this string at runtime.

    We all know that when we use html and css files we need to add these files in "Copy Bundle Resources". So that it will be available when html page load at runtime.

    So may be issue here is some of the functions in that html string are use some file or assistance before it loads. So that's why runtime html creation and loading is not working.

    How ever more of clear me if i'm misleading.

    You can set runtime values in html file though.

    Exmaple.html

    
    
    
    
    
    
    
    #latitude# 
    
    #longitude# 
    
    
    
    

    See above file, You can add marker like #latitude# and #longitude# in that file. And replace this at a runtime. Like below code.

    NSString *path = [[NSBundle mainBundle] pathForResource:@"filename" ofType:@"html"];
    NSString *html = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    
    html = [html stringByReplacingOccurrencesOfString:@"#latitude#" withString:];
    html = [html stringByReplacingOccurrencesOfString:@"#longitude#" withString:];
    

提交回复
热议问题