UIWebView Control for IOS development (19)

Source: Internet
Author: User

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:

 


 


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.