UIWebView显示网页

#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];

    webView.delegate = self;

    [self.view addSubview:webView];

    

    

    /*

     百度地图有关的网址http://api.map.baidu.com/marker?location=39.916979519873,116.41004950566&title=我的位置&content=百度

     奎科大厦&output=html

     

     http://map.baidu.com/mobile/webapp/index/index/?itj=45&wtj=wi

     

     NSString *string = [[NSBundle mainBundle]pathForResource:@"hhh.pdf" ofType:nil];

     

     //   - (void)loadRequest:(NSURLRequest *)request; 加载本地文件 或 网页

     

     //     网页视图加载的请求

     NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:string]];

     */

    //    网页视图需要一个加载的请求 加载的内容放在请求里面

    

#pragma mark-----------加载网页

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

    

    [webView loadRequest:request];

//    等待视图初始化

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

    

    [self.view addSubview:HUD];

}

//开始加载

- (void)webViewDidStartLoad:(UIWebView *)webView{

//    设置等待视图的提示文字

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

//    显示等待视图

    [HUD show:YES];

}

//加载完毕

- (void)webViewDidFinishLoad:(UIWebView *)webView{

//    隐藏等待试图

        [HUD hide:YES];

}

//加载失败

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

    

    HUD.labelText = [NSString stringWithFormat:@"error:网络连接失败"];

    //    NSLog(@"%@",error);

    //    显示等待视图

    [HUD show:YES];

    //    3秒后隐藏等待试图

    [HUD hide:YES afterDelay:3];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}

@end

原文地址:https://www.cnblogs.com/liumu/p/5308932.html