您的位置:首页 > 移动开发

UIWebView显示网页

2016-03-22 22:46 351 查看
#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: