分享

IOS开发(19)之UIWebView控件

 quasiceo 2014-04-11
IOS开发(19)之UIWebView控件
2013-05-06 14:38:39     我来说两句       作者:Developer_Man
收藏    我要投稿

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

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

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];//初始化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> 
 
@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;//页面适应手机大小 
    [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");
}
运行结果:

 


 

\

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多