首页 > 代码库 > 0521.使用UIWebView加载来自NetWork、Project、Documents的html、javascript文件

0521.使用UIWebView加载来自NetWork、Project、Documents的html、javascript文件


话不多少,直接上代码,思路还是挺简单的。


    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];
    
    // HTML文件来自Project
    // 步骤:path - > url - > request with url - > loadRequest
    NSString *path_Proj = [[NSBundle mainBundle]pathForResource:@"index" ofType:@"html"];
    NSURL *url_Proj = [NSURL fileURLWithPath:path_Proj];
    NSURLRequest *request_local = [NSURLRequest requestWithURL:url_Proj];
    [webView loadRequest:request_local];
    
    // HTML文件来自Documents
    // 步骤:pathArrary - > path - > url - > request with url - > loadRequest
    NSArray *pathArrary = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [pathArrary objectAtIndex:0];
    NSString *path_Doc = [documentsDirectory stringByAppendingPathComponent:@"index.html"];
    NSURL *url_Doc = [NSURL fileURLWithPath:path_Doc];
    NSURLRequest *request_Doc = [NSURLRequest requestWithURL:url_Doc];
    [webView loadRequest:request_Doc];
    
    // HTML文件来自Network
    // 步骤:url - > request with url - > loadRequest
    NSURL *url_net = [NSURL URLWithString:@"http://文件地址/index.html"];
    NSURLRequest *request_net = [NSURLRequest requestWithURL:url_net];
    [webView loadRequest:request_net];