Use of WebView for iOS Learning

Source: Internet
Author: User

Use of WebView for iOS Learning

1. initialize WebView

-(Void) viewDidLoad
{
[Super viewDidLoad];
WebView = [[UIWebView alloc] initWithFrame: CGRectMake (0, 0,320,480)];
NSURLRequest * request = [NSURLRequest requestWithURL: [NSURL URLWithString: @ http://www.baidu.com];
[Self. view addSubview: webView];
[WebView loadRequest: request];
}

2,

The implementation protocol is modified in ViewController. h as follows # import


@ Interface ViewController: UIViewController
{
UIWebView * webView;
}
@ End

Several important functions in UIWebView
1.-(void) webViewDidStartLoad :( UIWebView *) called when the webView page starts loading
2.-(void) webViewDidFinishLoad :( UIWebView *) called when the webView page is loaded
3.-(void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) Call when error webpage loading error

Add

[WebView setDelegate: self]; sets the proxy. In this way, the preceding three methods can be called back.

 

3. Load the wait page

-(Void) webViewDidStartLoad :( UIWebView *) webView
{
// Create a UIActivityIndicatorView with a translucent background
UIView * view = [[UIView alloc] initWithFrame: CGRectMake (0, 0,320,480)];
[View settag: 108];
[View setBackgroundColor: [UIColor blackColor];
[View setAlpha: 0.5];
[Self. view addSubview: view];

ActivityIndicator = [[UIActivityIndicatorView alloc] initWithFrame: CGRectMake (0.0f, 0.0f, 32.0f, 32.0f)];
[ActivityIndicator setCenter: view. center];
[ActivityIndicator setActivityIndicatorViewStyle: UIActivityIndicatorViewStyleWhite];
[View addSubview: activityIndicator];


[ActivityIndicator startAnimating];
}

When loading is complete or fails, remove the loading effect.

-(Void) webViewDidFinishLoad :( UIWebView *) webView
{
[ActivityIndicator stopAnimating];
UIView * view = (UIView *) [self. view viewWithTag: 108];
[View removeFromSuperview];
NSLog (@ webViewDidFinishLoad );


}
-(Void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) error
{
[ActivityIndicator stopAnimating];
UIView * view = (UIView *) [self. view viewWithTag: 108];
[View removeFromSuperview];

}

 

 


 

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.