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