load local HTML file when link is clicked in WebView

后端 未结 3 394
醉梦人生
醉梦人生 2021-01-15 23:49

I have a WebView that loads a local HTML file like this:

[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] path         


        
3条回答
  •  旧巷少年郎
    2021-01-16 00:34

    - (void)viewDidLoad {
        [super viewDidLoad];
        [webview loadHTMLString:[self htmlString] baseURL:[self baseURL]];
    }
    - (NSURL *)baseURL{
        NSString *htmlpath = [[NSBundle mainBundle] pathForResource:@"webpage" ofType:@"html"];
        return [[[NSURL alloc] initFileURLWithPath:htmlpath] autorelease];
    }
    
    - (NSString *)htmlString{
        NSError *error = nil;
        NSString *html = [[[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"webpage" ofType:@"html"] 
                                                         encoding:NSUTF8StringEncoding 
                                                            error:&error] autorelease];
        return html;
    }
    

提交回复
热议问题