iOS WebView

具體代碼:(需要使用到第三方等待視圖-MBProgressHUD,自己導入)

#import "ViewController.h"

#import "MBProgressHUD.h"

@interface ViewController ()<UIWebViewDelegate>


{

    MBProgressHUD *HUD;


}


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    


    

    

    

//    網頁視圖

    UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.frame];

    

    [self.view addSubview:webView];

    

         /*

//這是加載本地文件的

//   - (void)loadRequest:(NSURLRequest *)request    加載本地文件  或者網頁

    NSString *path = [[NSBundle mainBundle]pathForResource:@"123pdf" ofType:@"pdf"];

    

    NSURLRequest *resquest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:path]];

    

//  如果是網頁視圖  需要一個加載的請求  加載的內容  放在請求裏面

    

        */

    

    

    

//    加載網頁的

      NSURLRequest *resquest = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:@"https://www.baidu.com"]];

//    ***** 注意要有https://

    

    webView.delegate = self;

    

    [webView loadRequest:resquest];

    

    

    

    

    

    //    等待視圖

    HUD = [[MBProgressHUD alloc]initWithView:self.view];

    

    [self.view addSubview:HUD];

    

}



#pragma mark-WebView的代理方法

- (void)webViewDidStartLoad:(UIWebView *)webView

{

    [HUD show:YES];

    HUD.labelText = @"正在拼命加載";

    

}



- (void)webViewDidFinishLoad:(UIWebView *)webView

{

  

    [HUD hide:YES];

    

}



- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error

{

    NSLog(@"%@",error);

    

    

    HUD.labelText = [NSString stringWithFormat:@"錯誤:無網絡"];

    [HUD show:YES];

    [HUD hide:YES afterDelay:3];

    



}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


@end



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章