Use of UIWebView, simple browser implementation, uiwebview Browser
# Import "ViewController. h"
@ Interface ViewController () <UIWebViewDelegate>
@ Property (nonatomic, weak) UIWebView * web;
@ End
@ Implementation ViewController
-(Void) viewDidLoad {
[Super viewDidLoad];
[Self _ setTextField];
[Self _ loadWebView];
}
# Pragma mark-set the input box
-(Void) _ setTextField {
Self. tf. clearButtonMode = UITextFieldViewModeWhileEditing;
}
# Pragma mark-load WebView
-(Void) _ loadWebView {
UIWebView * web = [[UIWebView alloc] init];
Web. frame = CGRectMake (0, self. view. frame. size. height, self. view. frame. size. width, self. view. frame. size. height-42 );
Self. web = web;
Web. delegate = self;
[Self. view addSubview: web];
}
# Pragma mark-discard the first response
-(Void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event {
[Self. tf resignFirstResponder];
}
# Pragma mark-Action
-(IBAction) backAction :( id) sender {
[Self. web goBack];
// Self. web. frame = CGRectMake (0, self. view. frame. size. height, self. view. frame. size. width, self. view. frame. size. height-42 );
}
-(IBAction) forwardAction :( id) sender {
[Self. web goForward];
Self. web. frame = CGRectMake (0, 20, self. view. frame. size. width, self. view. frame. size. height-50 );
}
-(IBAction) undoAction :( id) sender {
[Self. web stopLoading];
}
-(IBAction) refreshAction :( id) sender {
[Self. web reload];
}
-(IBAction) searchAction :( id) sender {
[Self. tf resignFirstResponder];
Self. web. frame = CGRectMake (0, 20, self. view. frame. size. width, self. view. frame. size. height-60 );
[Self _ sendRequest];
}
# Pragma mark-UIWebViewDelegate
-(BOOL) webView :( UIWebView *) webView shouldStartLoadWithRequest :( NSURLRequest *) request navigationType :( UIWebViewNavigationType) navigationType {
Return YES;
}
-(Void) webViewDidStartLoad :( UIWebView *) webView {
}
-(Void) webViewDidFinishLoad :( UIWebView *) webView {
// NSLog (@ "% d = % d", [webView canGoBack], [webView canGoForward]);
Self. backBt. enabled = [webView canGoBack];
Self. forwardBt. enabled = [webView canGoForward];
Self. refresh. enabled = ([webView canGoBack] | [webView canGoForward]);
}
-(Void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) error {
NSLog (@ "% @", error );
}
-(Void) _ sendRequest {
// Http://www.baidu.com
NSURL * url = [NSURL URLWithString: self. tf. text];
NSURLRequest * request = [NSURLRequest requestWithURL: url];
[Self. web loadRequest: request];
}
@ End