UIWebView是iOS sdk中一个最常用的控件。是内置的浏览器控件,我们可以用它来浏览网页、打开文档等等。这篇文章我将使用这个控件,做一个简易的浏览器。如下图:
我们创建一个Window-based Application程序命名为:UIWebViewDemo
UIWebView的loadRequest可以用来加载一个url地址,它需要一个NSURLRequest参数。我们定义一个方法用来加载url。在UIWebViewDemoViewControl
- (void)loadWebPageWithString:(NSString*)urlString
{
NSURL *url =[NSURL URLWithString:urlString];
NSLog(urlString);
NSURLRequest *request =[NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
在界面上放置3个控件,一个textfield、一个button、一个uiwebview,布局如下:
在代码中定义相关的控件:webView用于展示网页、textField用于地址栏、activityIndicatorView用于加载的动画、buttonPress用于按钮的点击事件。
@interface UIWebViewDemoViewController :UIViewController { IBOutlet UIWebView *webView; IBOutlet UITextField *textField; UIActivityIndicatorView *activityIndicatorView; } - (IBAction)buttonPress:(id) sender; - (void)loadWebPageWithString:(NSString*)urlString; @end
使用IB关联他们。
设置UIWebView,初始化UIActivityIndicatorView:
- (void)viewDidLoad
{
[super viewDidLoad];
webView.scalesPageToFit =YES;
webView.delegate =self;
activityIndicatorView = [[UIActivityIndicatorView alloc]
initWithFrame : CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)] ;
[activityIndicatorView setCenter: self.view.center] ;
[activityIndicatorView setActivityIndicatorViewStyle: UIActivityIndicatorViewS tyleWhite] ;
[self.view addSubview : activityIndicatorView] ;
[self buttonPress:nil];
// Do any additional setup after loading the view from its nib.
}
UIWebView主要有下面几个委托方法:
1、- (void)webViewDidStartLoad:(UIWebView *)webView;开始加载的时候执行该方法。
2、- (void)webViewDidFinishLoad:(UIWebView *)webView;加载完成的时候执行该方法。
3、- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;加载出错的时候执行该方法。
我们可以将activityIndicatorView放置到前面两个委托方法中。
- (void)webViewDidStartLoad:(UIWebView *)webView
{
[activityIndicatorView startAnimating] ;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
[activityIndicatorView stopAnimating];
}
buttonPress方法很简单,调用我们开始定义好的loadWebPageWithString方法就行了:
- (IBAction)buttonPress:(id) sender { [textField resignFirstResponder]; [self loadWebPageWithString:textField.text]; }
当请求页面出现错误的时候,我们给予提示:
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error { UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" message:[error localizedDescription] delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; [alterview show]; [alterview release]; }
总结:本文通过实现一个简单的浏览器,说明了uiwebview的方法和属性,相信通过这个例子,应该明白uiwebview的使用了。
设置UIWebView透明
- [webview
setBackgroundColor:[UIColor clearColor]]; -
- webview.opaque
= NO;
禁止UIWebView滚动
- webView.scrollView.bounces
= NO; //__IPHONE_5_0
- UIScrollView
*scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0]; - scrollView.bounces
= NO;
获取UIWebView高度
- -
(void)webViewDidFinishLoad:(UIWebView *)webView1 - {
-
UIScrollView *scrollView = (UIScrollView *)[[webView subviews] objectAtIndex:0]; -
CGFloat webViewHeight = [scrollView contentSize].height; -
NSString *curHeight = [webView stringByEvaluatingJavaSc riptFromString:@"document.body.scrollHeight;"]; -
-
CGRect newFrame = webView.frame; -
newFrame.size.height = webViewHeight; -
webView.frame = newFrame; - }
使用JS给UIWebView添加事件响应
1.首先定义事件的JavaScript
- //
timeStamp 微秒 - static
NSString * const webTouchJavaScriptString = -
@">document.ontouchstart=function(event){\ -
x=event.targetTouches[0].clientX;\ -
y=event.targetTouches[0].clientY;\ -
time=event.timeStamp;\ -
document.location=\"wiweb:touch:start:"+x+":"+y+":"+time;};\ -
document.ontouchmove=function(event){\ -
x=event.targetTouches[0].clientX;\ -
y=event.targetTouches[0].clientY;\ -
document.location=\"wiweb:touch:move:"+x+":"+y;};\ -
document.ontouchcancel=function(event){\ -
document.location=\"wiweb:touch:cancel";};\ -
document.ontouchend=function(event){\ -
time=event.timeStamp;\ -
document.location=\"wiweb:touch:end:"+time;}; ";
2.组织字符串
- NSString
*webviewText = @""; - NSString
*htmlString = [webviewText stringByAppendingFormat:@"%@", @"自定SDFSDFSDFSDF义字体fsdgjdlagj asdkgjksdh卡号给卡仕达;逛了会街啊啊流口水 http://www.baidu.com 的感觉卡拉;四大金刚;拉开始打工绿卡;但是结果来看;就爱上的看过就卡的;上来讲赶快来;啊都是经过后ihgoiadsg;肯定是噶上的好;拉克丝的价格爱国阿斯顿改了可"]; -
- NSString
*newHTMLString=[htmlString stringByAppendingString:webTouchJavaScriptString ];
3.事件响应
- -
(BOOL)webView:(UIWebView *)webView shouldStartLoadWithReque st:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType - {
-
BOOL result = YES; -
NSURL *requestURL =[[request URL] retain]; -
NSString *requestString = [[request URL] absoluteString]; -
static BOOL bstart = NO; -
static BOOL bmove = NO; -
static double lasttime = 0; -
-
NSString *str = [requestURL scheme]; -
if ( ([str isEqualToString:@"http"] || [str isEqualToString:@"https"] || [str isEqualToString:@"mailto"] || [str isEqualToString:@"tel"]) -
&& (navigationType == UIWebViewNavigationTypeL inkClicked) ) -
{ -
result = ![[UIApplication sharedApplication] openURL:[requestURL autorelease]]; -
} -
else -
{ -
[requestURL release]; -
NSArray *components = [requestString componentsSeparatedByStr ing:@":"]; -
if ([components count] > 2 -
&& [(NSString *)[components objectAtIndex:0] isEqualToString:@"wiweb"] -
&& [(NSString *)[components objectAtIndex:1] isEqualToString:@"touch"]) -
{ -
NSString *eventString=[components objectAtIndex:2]; -
if ([eventString isEqualToString:@"start"]) -
{ -
float pointX=[[components objectAtIndex:3] floatValue]; -
float pointY=[[components objectAtIndex:4] floatValue]; -
double time=[[components objectAtIndex:5] doubleValue]; -
CGPoint aPoint = CGPointMake(pointX, pointY); -
NSLog(@"start: %@", NSStringFromCGPoint(aPoint)); -
NSLog(@"start time: interval: ", time/1000, (time - lasttime)/1000); -
lasttime = time; -
bstart = YES; -
bmove = NO; -
NSLog(@"bstart: %d -- bmove: %d", bstart, bmove); -
} -
else if ([eventString isEqualToString:@"move"]) -
{ -
float pointX=[[components objectAtIndex:3] floatValue]; -
float pointY=[[components objectAtIndex:4] floatValue]; -
CGPoint aPoint=CGPointMake(pointX, pointY); -
NSLog(@"move: %@", NSStringFromCGPoint(aPoint)); -
bmove = YES; -
NSLog(@"bstart: %d -- bmove: %d", bstart, bmove); -
} -
else if ([eventString isEqualToString:@"cancel"]) -
{ -
NSLog(@"cancel"); -
bstart = NO; -
bmove = NO; -
NSLog(@"bstart: %d -- bmove: %d", bstart, bmove); -
} -
else if ([eventString isEqualToString:@"end"]) -
{ -
double time=[[components objectAtIndex:3] doubleValue]; -
NSLog(@"end"); -
NSLog(@"bstart: %d -- bmove: %d", bstart, bmove); -
NSLog(@"end time: interval: ", time/1000, (time - lasttime)/1000); -
if (bstart && !bmove) -
{ -
if (time - lasttime > 400) -
{ -
NSLog(@"LongPress!!!!!!"); -
} -
else -
{ -
NSLog(@"Click!!!!!!"); -
} -
} -
bstart = NO; -
bmove = NO; -
} -
-
return NO; -
} -
} -
-
NSURL *url = [request URL]; -
NSString *curUrl= [url absoluteString]; -
NSLog(@"cururl: %@", curUrl); -
-
return result; - }