http://iliunian.diandian.com/?tag=uiwebview
HTTP cookies for connections and UIWebViews
+ (void) setCookie:(NSString *)cookieName:(NSString *)cookieValue:(NSString *)cookieDomain
{
NSDictionary *properties = [NSDictionary dictionaryWithObjectsAndKeys:
cookieDomain, NSHTTPCookieDomain,
cookieName, NSHTTPCookieName,
cookieValue, NSHTTPCookieValue,
@"/", NSHTTPCookiePath,
nil];
[[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:[NSHTTPCookie cookieWithProperties:properties]];
http://uiwebview.saoqiao.com UIWebView清空
页面适应webView大小的方法
一、页面适应webView大小的方法:
在网页中使用css控制页面显示宽度,并根据iPad/iPhone不同的分辨率或webView实际的宽度对图片进行缩放处理。
2、不对网页进行处理,使用webView的参数控制,使网页适应webView的宽度,参数设置: webView.scalesPageToFit = YES;
二、要想控制webView调用webViewDidStartLoad和webViewDidFinishLoad 就是在网页加载前、加载后做一些其他动作(例如调用UIActivityIndicatorView),必须先在viewDidLoad中调用 webView.delegate = self; 然后在退出应用前做如下释放动作: webView.delegate = nil;
UIWebView cookie相关
NSHTTPCookieStorage *sharedHTTPCookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookies = [sharedHTTPCookieStorage cookiesForURL:[NSURL URLWithString:theURL]];
NSEnumerator *enumerator = [cookies objectEnumerator];
NSHTTPCookie *cookie;
while (cookie = [enumerator nextObject]) {
NSLog(@"COOKIE{name: %@, value: %@}", [cookie name], [cookie value]);
}
UIWebView 离线缓存
相信不少朋友用过UIWebView,webView下载的图片一般比较大,这个要能缓存就好了,可以大幅度提高加载速度,同时为用户节省流量。本文就是讲如何完美解决webView缓存的问题。
实际上,UIWebView自己是有缓存的,但容量有限,清理时间我们也不好掌握,那它是用什么做的缓存呢?是NSURLCache。看到它有几个方法:
+ (void)setSharedURLCache:(NSURLCache *)cache;
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request;
- (void)storeCachedResponse:(NSCachedURLResponse *)cachedResponse forRequest:(NSURLRequest *)request;
太好了,我们只要写一个子类继承NSURLCache,实现后两个方法,再让这个子类对象成为sharedURLCache,就可以操控webView的请求和缓存了。
代码块:
原来我自定义过UITextView,然后将捕获touchesBegan,touchesMoved和touchesEnded的事件后交给父层去处理,这样就可以UITextView上进行手势翻页等处理,今天想要故伎重演在UIWebView上,结果失败了。后来用了一种最最简单的办法实现了。
你猜对了,是UITapGestureRecognizer,贴上代码。
在UIViewController中,加入协议UIGestureRecognizerDelegate,然后.m文件里加入以下代码:
UITapGestureRecognizer* singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; [self.view addGestureRecognizer:singleTap]; //这个可以加到任何控件上,比如你只想响应WebView,我正好填满整个屏幕 singleTap.delegate = self; singleTap.cancelsTouchesInView = NO; [singleTap release];
然后有一个关键的,要实现一个方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer { return YES; }
最后,响应的方法中,可以获取点击的坐标哦!
-(void)handleSingleTap:(UITapGestureRecognizer *)sender{ CGPoint point = [sender locationInView:self.view]; NSLog(@"handleSingleTap!pointx:%f,y:%f",point.x,point.y); }
好了,得到启发了吧,也能获取到坐标了。不过如果你要直接判断手势方向之类的,比如向左或向右轻扫,可以使用UISwipeGestureRecognizer类
UIWebView获取点击连接
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithReque
//enum {
//
//
//
//
//
//
//};
//typedef NSUInteger UIWebViewNavigationType;
//判断是否是单击
if (navigationType == UIWebViewNavigationTypeL
NSURL *url = [request URL];
NSLog(@”cururl……………%@”,curUrl);
}
return YES;
}
id internalWebView=[[myWebView _documentView] webView];
[internalWebView setMaintainsBackForwardList:NO];
[internalWebView setMaintainsBackForwardList:YES];
清空UIWebView的缓存
[[NSURLCache sharedURLCache] removeCachedResponseForRequest:NSURLRequest];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
for(NSHTTPCookie *cookie in [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies]) { }
UIWebView清除Cookie:
//清除cookies NSHTTPCookie *cookie; NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; for (cookie in [storage cookies]) { [storage deleteCookie:cookie]; }UIWebView清除缓存:
//清除UIWebView的缓存 [[NSURLCachesharedURLCache] removeAllCachedResponses];隐藏滚动条和上下滚动时出边界的后面的黑色的背景
- aWebView.backgroundColor=[UIColor clearColor];
- for (UIView *_aView in [aWebView subviews])
- {
- if ([_aView isKindOfClass:[UIScrollView class]])
- {
- [(UIScrollView *)_aView setShowsVerticalScrollIndicator:NO]; //右侧的滚动条
- for (UIView *_inScrollview in _aView.subviews)
- {
- if ([_inScrollview isKindOfClass:[UIImageView class]])
- {
- _inScrollview.hidden = YES; //上下滚动出边界时的黑色的图片
- }
- }
- }
- }
UIWebView更改字体大小
方法一:
1 - (void)webViewDidFinishLoad:(UIWebView *)webView 2 { 3 // finished loading, hide the activity indicator in the status bar 4 [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 5 [self.guideWebView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextSizeAdjust= ’150%’"]; 6 }
方法二:
1 | [ NSString stringWithFormat:@ "<html> \n" |
2 | "<head> \n" |
3 | "<style type=\"text/css\"> \n" |
4 | "body {font-family: \"%@\"; font-size: %f; color: %@;}\n" |
5 | "</style> \n" |
6 | "</head> \n" |
7 | "<body>%@</body> \n" |
8 | "</html>" , @ "宋体" , fontSize,fontColor,stringValue] ; |
改变字体颜色
1 | NSString *jsString = [[ NSString alloc] initWithFormat:@ "document.body.style.fontSize=%f;document.body.style.color=%@" ,fontSize,fontColor]; |
2 | [webView stringByEvaluatingJavaScriptFromString:jsString]; |
3 | [jsString release]; |