IOS uiwebview中加载服务器上的html图片不显示
2015-06-24 15:16
417 查看
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];
}
所以我在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];
}
相关文章推荐
- 分享 APP竞品分析的几个工具
- android 引用lib里面的jar找不到类
- Android Touch事件传递机制解析
- 解决tableView上cell的按钮点击效果不明显(轻点无效)
- IOS 轻量级视频播放器
- 一种在android中实现MVP模式的新思路
- 一个封装好的iOS无限滚动组件HXInfiniteScrollView
- 自定义 URL Scheme 完全指南
- Android广播机制:Broadcast
- Launch Screen在iOS7/8中的实现
- 关于 ViewGroup 下 onDraw 方法失效
- Ubuntu14.04 Android 源码编译
- adb 命令之appt
- android开发使用fragment commit时的一些坑
- Android HAL实例解析
- ios中 继承对象模型的归档实现
- Android29_Loader
- Android28_Bitmap二次采样
- 微信公众平台开发(102) 模板消息
- Unity 3D UV实现小地图