IOS uiwebview中加载服务器上的html图片不显示

uiwebview显示服务器上的html,图片显示成问号,在电脑上用浏览器打开,图片显示403。但是将html保存到本地后打开就可以显示。

所以我在IOS中也是将服务器返回的html内容保存到了本地,然后加载到uiwebview中,可以显示。

- (void)viewDidLoad {
    [super viewDidLoad];
    
    webViews.delegate=self;
    //自适应宽度
    webViews.scalesPageToFit=YES;
    
    webViews.backgroundColor=[UIColor whiteColor];
    webViews.scrollView.bounces=YES;
    
    //iOS7 NavigationBar覆盖内容
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.translucent = NO;
    [self clearBlackBottom];
    
    //清除缓存
    NSHTTPCookie *cookie;
    NSHTTPCookieStorage *storage=[NSHTTPCookieStorage sharedHTTPCookieStorage];
    for (cookie in [storage cookies]) {
        [storage deleteCookie:cookie];
    }
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
    
    //因为一些网页上的图片在线显示不出来,我就先将html内容保存到本地 然后加载本地文件
    //获取文件路径
    //沙盒中文件
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *filePath=[path stringByAppendingPathComponent:@"detail.html"];
    //本地文件
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"detail" ofType:@"html"];

    //待写入的数据
    //从服务器获取数据
    NSString *string = [NSString stringWithFormat:@"%@docdetail/detail?doc_id=%@", BaseURLString,self.data];
    NSString *htmlResponseStr=[NSString stringWithContentsOfURL:[NSURL URLWithString:string] encoding:NSUTF8StringEncoding error:nil];
    //创建数据缓冲
    NSMutableData *writer = [[NSMutableData alloc] init];
    //将字符串添加到缓冲中
    [writer appendData:[htmlResponseStr dataUsingEncoding:NSUTF8StringEncoding]];
    //将缓冲的数据写入到文件中
    [writer writeToFile:filePath atomically:YES];
    
    //加载本地文件
    NSURL *localUrl = [NSURL fileURLWithPath:filePath];
    NSURLRequest *localRequest = [NSURLRequest requestWithURL:localUrl];
    //打开网页
    [webViews loadRequest:localRequest];
    
}



  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值