1 Preface
The UIWebView control can correctly and dynamically load Web pages. We can use the UIWebView class to drive all the permissions of Safari on IOS.
2. code example
Custom UIWebView content:
ZYViewController. h:
[Plain]
# Import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController
@ Property (nonatomic, strong) UIWebView * myWebView;
@ End
# Import <UIKit/UIKit. h>
@ Interface ZYViewController: UIViewController
@ Property (nonatomic, strong) UIWebView * myWebView;
@ End
ZYViewController. m:
[Plain]
@ Synthesize myWebView;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyWebView = [[UIWebView alloc] initWithFrame: self. view. bounds]; // initialize UIWebView
[Self. view addSubview: myWebView];
NSString * htmlString = @ "IOS 5 Programming <strong> Cookbook </strong>"; // set the content of UIWebView
[MyWebView loadHTMLString: htmlString baseURL: nil]; // load content
}
@ Synthesize myWebView;
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
Self. view. backgroundColor = [UIColor whiteColor];
MyWebView = [[UIWebView alloc] initWithFrame: self. view. bounds]; // initialize UIWebView
[Self. view addSubview: myWebView];
NSString * htmlString = @ "IOS 5 Programming <strong> Cookbook </strong>"; // set the content of UIWebView
[MyWebView loadHTMLString: htmlString baseURL: nil]; // load content
}
Running result:
Load existing url content:
ZYUIWebViewController. h:
[Plain]
# Import <UIKit/UIKit. h>
@ Interface ZYUIWebViewController: UIViewController <UIWebViewDelegate>
@ Property (nonatomic, strong) UIWebView * myWebView;
@ End
# Import <UIKit/UIKit. h>
@ Interface ZYUIWebViewController: UIViewController <UIWebViewDelegate>
@ Property (nonatomic, strong) UIWebView * myWebView;
@ End
ZYUIWebViewController. m:
[Plain]
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view.
Self. view. backgroundColor = [UIColor whiteColor];
Self. myWebView = [[UIWebView alloc] initWithFrame: self. view. bounds];
Self. myWebView. scalesPageToFit = YES; // adjust the page size of your mobile phone
[Self. view addSubview: self. myWebView];
NSURL * url = [NSURL URLWithString: @ "http://www.csdn.net"]; // Initialize an NSURL object
NSURLRequest * request = [NSURLRequest requestWithURL: url]; // initialize the NSURLRequest object
Self. myWebView. delegate = self; // sets the proxy.
[Self. myWebView loadRequest: request]; // load the request object
}
// Start loading Web pages
-(Void) webViewDidStartLoad :( UIWebView *) webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: YES]; // set the progress bar to start
NSLog (@ "Loading webViewDidStartLoad method ");
}
// End recording
-(Void) webViewDidFinishLoad :( UIWebView *) webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO]; // set the progress bar to end
NSLog (@ "Loading webViewDidFinishLoad method ");
}
// When loading fails, for example, a network exception occurs.
-(Void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];
NSLog (@ "Loading webView method ");
}
-(Void) viewDidLoad
{
[Super viewDidLoad];
// Do any additional setup after loading the view.
Self. view. backgroundColor = [UIColor whiteColor];
Self. myWebView = [[UIWebView alloc] initWithFrame: self. view. bounds];
Self. myWebView. scalesPageToFit = YES; // adjust the page size of your mobile phone
[Self. view addSubview: self. myWebView];
NSURL * url = [NSURL URLWithString: @ "http://www.csdn.net"]; // Initialize an NSURL object
NSURLRequest * request = [NSURLRequest requestWithURL: url]; // initialize the NSURLRequest object
Self. myWebView. delegate = self; // sets the proxy.
[Self. myWebView loadRequest: request]; // load the request object
}
// Start loading Web pages
-(Void) webViewDidStartLoad :( UIWebView *) webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: YES]; // set the progress bar to start
NSLog (@ "Loading webViewDidStartLoad method ");
}
// End recording
-(Void) webViewDidFinishLoad :( UIWebView *) webView {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO]; // set the progress bar to end
NSLog (@ "Loading webViewDidFinishLoad method ");
}
// When loading fails, for example, a network exception occurs.
-(Void) webView :( UIWebView *) webView didFailLoadWithError :( NSError *) error {
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];
NSLog (@ "Loading webView method ");
}
Running result: