美文网首页
UIWebView加载本地html

UIWebView加载本地html

作者: Leafly | 来源:发表于2017-09-26 11:11 被阅读140次

    UIWebView除了能加载网页地址外还可以加载本地html,加载的方式主要有两种

    读取本地html内容,加载内容

    NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
    NSString *htmlPath = [NSString stringWithFormat:@"%@/index.html", mainBundlePath];
    NSString *htmlContent = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    [_webView loadHTMLString: htmlContent baseURL:baseURL];
    

    通过本地html地址加载内容

    NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
    NSURLRequest *request = [NSURLRequest requestWithURL:baseURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
    [_webView loadRequest:request];
    
    

    有些情况下,我们不仅要能加载本地html,还需要给本地html传递参数,这种情况下我们只能通过方法二来进行参数传递

    NSString *mainBundlePath = [[NSBundle mainBundle] bundlePath];
    NSURL *baseURL = [NSURL URLWithString:mainBundlePath];
    NSString *queryString = [NSString stringWithFormat:@"%@/index.html?key=value", mainBundlePath];
    NSURL *queryURL = [NSURL URLWithString:queryString];
    NSURLRequest *request = [NSURLRequest requestWithURL:queryURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:(NSTimeInterval)10.0 ];
    [_webView loadRequest:request];
    

    相关文章

      网友评论

          本文标题:UIWebView加载本地html

          本文链接:https://www.haomeiwen.com/subject/sdhsextx.html