uiwebview显示服务器上的html,图片显示成问号,在电脑上用浏览器打开,图片显示403。但是将html保存到本地后打开就可以显示。
所以我在IOS中也是将服务器返回的html内容保存到了本地,然后加载到uiwebview中,可以显示。
[objc] view plaincopy
- - (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];
- }