uiwebview load date

- (void)viewDidLoad  
{  
    [super viewDidLoad];  
    [self setupUI];  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.docx" ofType:nil];  
    NSURL *url = [NSURL fileURLWithPath:path];  
    NSLog(@"%@", [self mimeType:url]);  


    //webview加载本地文件,可以使用加载数据的方式  
    //第一个诶参数是一个NSData, 本地文件对应的数据  
    //第二个参数是MIMEType  
    //第三个参数是编码格式  
    //相对地址,一般加载本地文件不使用,可以在指定的baseURL中查找相关文件。  

    //以二进制数据的形式加载沙箱中的文件,  
    NSData *data = [NSData dataWithContentsOfFile:path];  

    [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];  
}  

#pragma mark 加载docx文件  
- (void)loadDOCX  
{  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.docx" ofType:nil];  
    NSURL *url = [NSURL fileURLWithPath:path];  
    NSLog(@"%@", [self mimeType:url]);  

    NSData *data = [NSData dataWithContentsOfFile:path];  

    [self.webView loadData:data MIMEType:@"application/vnd.openxmlformats-officedocument.wordprocessingml.document" textEncodingName:@"UTF-8" baseURL:nil];}  

#pragma mark 加载pdf文件  
- (void)loadPDF  
{  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"iOS6Cookbook.pdf" ofType:nil];  
    NSURL *url = [NSURL fileURLWithPath:path];  
    NSLog(@"%@", [self mimeType:url]);  

    NSData *data = [NSData dataWithContentsOfFile:path];  

    [self.webView loadData:data MIMEType:@"application/pdf" textEncodingName:@"UTF-8" baseURL:nil];  
}  

#pragma mark 加载本地文本文件  
- (void)loadText  
{  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"关于.txt" ofType:nil];  
    NSURL *url = [NSURL fileURLWithPath:path];  
    NSLog(@"%@", [self mimeType:url]);  

    NSData *data = [NSData dataWithContentsOfFile:path];  

    [self.webView loadData:data MIMEType:@"text/plain" textEncodingName:@"UTF-8" baseURL:nil];  
}  


#pragma mark 加载本地html文件  
- (void)loadHTML  
{  
    NSString *path = [[NSBundle mainBundle] pathForResource:@"demo.html" ofType:nil];  
    NSURL *url = [NSURL fileURLWithPath:path];  
    NSLog(@"%@", [self mimeType:url]);  

    NSData *data = [NSData dataWithContentsOfFile:path];  

    [self.webView loadData:data MIMEType:@"text/html" textEncodingName:@"UTF-8" baseURL:nil];  
}  


#pragma mark 获取指定URL的MIMEType类型  
- (NSString *)mimeType:(NSURL *)url  
{  
    //1NSURLRequest  
    NSURLRequest *request = [NSURLRequest requestWithURL:url];  
    //2NSURLConnection  

    //3 在NSURLResponse里,服务器告诉浏览器用什么方式打开文件。  

    //使用同步方法后去MIMEType  
    NSURLResponse *response = nil;  
    [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];  
    return response.MIMEType;  
}  

你可能感兴趣的:(iOS)