Use of UIWebView, simple browser implementation, uiwebview Browser

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.