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

iOS WebView

2015-10-09 21:25 92 查看
具体代码:(需要使用到第三方等待视图-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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: