- (
instancetype
)initWithNibName:(
NSString
*)nibNameOrNil bundle:(
NSBundle
*)nibBundleOrNil
{
self = [ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];
if ( self )
{
self . hidesBottomBarWhenPushed = YES ;
}
return self ;
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
{
self = [ super initWithNibName :nibNameOrNil bundle :nibBundleOrNil];
if ( self )
{
self . hidesBottomBarWhenPushed = YES ;
}
return self ;
}
- ( void )viewDidLoad {
[ super viewDidLoad ];
//
新建网页视图
webView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
// 网页视图自适应屏幕尺寸
webView . scalesPageToFit = YES ;
// 设置代理
webView . delegate = self ;
webView = [[ UIWebView alloc ] initWithFrame : self . view . bounds ];
// 网页视图自适应屏幕尺寸
webView . scalesPageToFit = YES ;
// 设置代理
webView . delegate = self ;
// //
加载
百度
网页
// //
创建
request
对象
// NSMutableURLRequest *mRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];
//
// // 网页控件加载一个网络请求
// [webView loadRequest:mRequest];
[ self . view addSubview : webView ];
// 初始化风火轮
UIActivityIndicatorView *activity = [[ UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleWhite ];
UIBarButtonItem *rightBtn = [[ UIBarButtonItem alloc ] initWithCustomView :activity];
self . navigationItem . rightBarButtonItem = rightBtn;
[ self _loadData ];
//
// // 网页控件加载一个网络请求
// [webView loadRequest:mRequest];
[ self . view addSubview : webView ];
// 初始化风火轮
UIActivityIndicatorView *activity = [[ UIActivityIndicatorView alloc ] initWithActivityIndicatorStyle : UIActivityIndicatorViewStyleWhite ];
UIBarButtonItem *rightBtn = [[ UIBarButtonItem alloc ] initWithCustomView :activity];
self . navigationItem . rightBarButtonItem = rightBtn;
[ self _loadData ];
}
//
加载本地
html
网页
- (
void
)_loadData
{
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"news" ofType : @"html" ];
NSError *error = nil ;
NSString *str = [ NSString stringWithContentsOfFile :filePath encoding : NSUTF8StringEncoding error :&error];
NSDictionary *dic = [ DataServiece requestData : news_detail ];
NSString *title = [dic objectForKey : @"title" ];
NSString *source = [dic objectForKey : @"source" ];
NSString *time = [dic objectForKey : @"time" ];
NSString *content = [dic objectForKey : @"content" ];
NSString *author = [dic objectForKey : @"author" ];
NSString *htmlStr = [ NSString stringWithFormat :str,title,source,time,content,author];
[ webView loadHTMLString :htmlStr baseURL : nil ];
}
#pragma mark -UIWebViewDelegate
// 将要加载某个请求
- ( BOOL )webView:( UIWebView *)webView shouldStartLoadWithRequest:( NSURLRequest *)request navigationType:( UIWebViewNavigationType )navigationType
{
return YES ;
}
// 已经开始加载
- ( void )webViewDidStartLoad:( UIWebView *)webView
{
UIActivityIndicatorView *activity = ( UIActivityIndicatorView *) self . navigationItem . rightBarButtonItem . customView ;
[activity startAnimating ];
}
// 结束加载
- ( void )webViewDidFinishLoad:( UIWebView *)webView
{
UIActivityIndicatorView *activity = ( UIActivityIndicatorView *) self . navigationItem . rightBarButtonItem . customView ;
[activity stopAnimating ];
}
// 加载出错
- ( void )webView:( UIWebView *)webView didFailLoadWithError:( NSError *)error
{
{
NSString *filePath = [[ NSBundle mainBundle ] pathForResource : @"news" ofType : @"html" ];
NSError *error = nil ;
NSString *str = [ NSString stringWithContentsOfFile :filePath encoding : NSUTF8StringEncoding error :&error];
NSDictionary *dic = [ DataServiece requestData : news_detail ];
NSString *title = [dic objectForKey : @"title" ];
NSString *source = [dic objectForKey : @"source" ];
NSString *time = [dic objectForKey : @"time" ];
NSString *content = [dic objectForKey : @"content" ];
NSString *author = [dic objectForKey : @"author" ];
NSString *htmlStr = [ NSString stringWithFormat :str,title,source,time,content,author];
[ webView loadHTMLString :htmlStr baseURL : nil ];
}
#pragma mark -UIWebViewDelegate
// 将要加载某个请求
- ( BOOL )webView:( UIWebView *)webView shouldStartLoadWithRequest:( NSURLRequest *)request navigationType:( UIWebViewNavigationType )navigationType
{
return YES ;
}
// 已经开始加载
- ( void )webViewDidStartLoad:( UIWebView *)webView
{
UIActivityIndicatorView *activity = ( UIActivityIndicatorView *) self . navigationItem . rightBarButtonItem . customView ;
[activity startAnimating ];
}
// 结束加载
- ( void )webViewDidFinishLoad:( UIWebView *)webView
{
UIActivityIndicatorView *activity = ( UIActivityIndicatorView *) self . navigationItem . rightBarButtonItem . customView ;
[activity stopAnimating ];
}
// 加载出错
- ( void )webView:( UIWebView *)webView didFailLoadWithError:( NSError *)error
{
}