当前位置:编程学习 > wap >>

IOS开发(19)之UIWebView控件

1 前言
UIWebView控件可以正确的动态加载Web页面,我们可以通过UIWebView类行驶IOS上Safari的所有权限。

2 代码实例
自定义UIWebView内容:

ZYViewController.h:


[plain]
#import <UIKit/UIKit.h> 
 
@inte易做图ce ZYViewController : UIViewController 
 
@property(nonatomic,strong) UIWebView *myWebView; 
 
@end 

#import <UIKit/UIKit.h>

@inte易做图ce 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];//初始化UIWebView 
    [self.view addSubview:myWebView]; 
    NSString *htmlString = @"IOS 5 Programming <strong>Cookbook</strong>";//设置UIWebView的内容 
    [myWebView loadHTMLString:htmlString baseURL:nil];//加载内容 

@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];//初始化UIWebView
    [self.view addSubview:myWebView];
    NSString *htmlString = @"IOS 5 Programming <strong>Cookbook</strong>";//设置UIWebView的内容
    [myWebView loadHTMLString:htmlString baseURL:nil];//加载内容
}
运行结果:

 \
 


加载已有的url内容:

ZYUIWebViewController.h:

 

[plain]
#import <UIKit/UIKit.h> 
 
@inte易做图ce ZYUIWebViewController : UIViewController<UIWebViewDelegate> 
 
@property(nonatomic,strong) UIWebView *myWebView; 
 
@end 

#import <UIKit/UIKit.h>

@inte易做图ce 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;//页面适应手机大小 
    [self.view addSubview:self.myWebView]; 
    NSURL *url = [NSURL URLWithString:@"http://www.csdn.net"];//初始化NSURL对象 
    NSURLRequest *request = [NSURLRequest requestWithURL:url];//初始化NSURLRequest对象 
    self.myWebView.delegate = self;//设置代理 
    [self.myWebView loadRequest:request];//加载request对象 

//开始加载Web页面的时候 
-(void)webViewDidStartLoad:(UIWebView *)webView{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];//设置进度条开始 
    NSLog(@"Loading webViewDidStartLoad method"); 

//结束记载的时候 
-(void)webViewDidFinishLoad:(UIWebView *)webView{ 
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];//设置进度条结束 
    NSLog(@"Loading webViewDidFinishLoad method"); 

//加载失败的时候,如:网络异常 
-(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;//页面适应手机大小
    [self.view addSubview:self.myWebView];
    NSURL *url = [NSURL URLWithString:@"http://www.csdn.net"];//初始化NSURL对象
    NSURLRequest *request = [NSURLRequest requestWithURL:url];//初始化NSURLRequest对象
    self.myWebView.delegate = self;//设置代理
    [self.myWebView loadRequest:request];//加载request对象
}
//开始加载Web页面的时候
-(void)webViewDidStartLoad:(UIWebView *)webView{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];//设置进度条开始
    NSLog(@"Loading webViewDidStartLoad method");
}
//结束记载的时候
-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];//设置进度条结束
    NSLog(@"Loading webViewDidFinishLoad method");
}
//加载失败的时候,如:网络异常
-(void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
    NSLog(@"Loading webView method");
}
运行结果:

 


 

\
补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,