iOS - UIWebView 展示txt 文件 解决乱码及

2018-08-24  本文已影响0人  HanZhiZzzzz
    UIWebView *webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, Kk_NaivagationMargin, self.view.bounds.size.width, self.view.bounds.size.height - Kk_NaivagationMargin)];
    webView.delegate = self;
    webView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:webView];
    if ([self.fileType isEqualToString:@"txt"]) {
        NSData *txtData = [NSData dataWithContentsOfFile:fileUrl];
        //自定义一个编码方式
        [webView loadData:txtData MIMEType:@"text/txt" textEncodingName:@"GBK" baseURL:nil];
    }else{
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileUrl]];
        NSMutableURLRequest *mutableRequest = [request mutableCopy];
        mutableRequest.timeoutInterval = 60;
        request = [mutableRequest copy];        //拷贝回去
        [webView loadRequest:request];
    }
    self.webView = webView;
上一篇下一篇

猜你喜欢

热点阅读