你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(19)之UIWebView控件

IOS開發(19)之UIWebView控件

編輯:IOS開發綜合

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");
}
運行結果:

 


 

\
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved