UIWebView

29 篇文章 0 订阅

http://iliunian.diandian.com/?tag=uiwebview 

HTTP cookies for connections and UIWebViews

A very common requirement when connecting to a server from the iPhone or within a UIWebView is the ability to set a cookie. Cookies are often used for authentication in particular. Fortunately there is a nice way to set a global cookie for any external connections your app makes using NSHttpCookieStorage. The function below sets a global cookie with the specified domain, name and value (the path used is /): 

+ (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 shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType { 
//enum { 
//  UIWebViewNavigationTypeLinkClicked, 
//   UIWebViewNavigationTypeFormSubmitted, 
//   UIWebViewNavigationTypeBackForward, 
//   UIWebViewNavigationTypeReload, 
//   UIWebViewNavigationTypeFormResubmitted, 
//   UIWebViewNavigationTypeOther 
//}; 
//typedef NSUInteger UIWebViewNavigationType; 

//判断是否是单击
if (navigationType == UIWebViewNavigationTypeLinkClicked) { 
NSURL *url = [request URL]; 
       nsstring *curUrl= [url absoluteString]; 
NSLog(@”cururl……………%@”,curUrl); 


return YES;   

UIWebView清空历史网页

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];

隐藏滚动条和上下滚动时出边界的后面的黑色的背景

  1. aWebView.backgroundColor=[UIColor clearColor];
  2.    for (UIView *_aView in [aWebView subviews])
  3.    {
  4.     if ([_aView isKindOfClass:[UIScrollView class]])
  5.        {
  6.            [(UIScrollView *)_aView setShowsVerticalScrollIndicator:NO]; //右侧的滚动条
  7.         for (UIView *_inScrollview in _aView.subviews)
  8.            {
  9.                if ([_inScrollview isKindOfClass:[UIImageView class]])
  10.                {
  11.                    _inScrollview.hidden = YES;  //上下滚动出边界时的黑色的图片
  12.                }
  13.         }
  14.     }
  15. }

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];

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值