WebView,嘿嘿,懒人专用,直接复制粘贴就能用,frame可调
2016-05-04 16:53
453 查看
在控制器的.h文件中
#import <UIKit/UIKit.h>
#import "FirstView.h"
@interface FirstViewController :
UIViewController<UIWebViewDelegate> {
UIWebView *web;
UIActivityIndicatorView *activityIndicatorView;
UIView *opaqueView;
}
@property (nonatomic,
strong) FirstView *fv;
@end
在.m中,如下:
#import "FirstViewController.h"
@interface
FirstViewController ()
@end
@implementation FirstViewController
- (void)loadView {
[super loadView];
self.fv = [[FirstView
alloc] initWithFrame:[UIScreen
mainScreen].bounds];
self.view =
self.fv;
}
- (void)viewWillAppear:(BOOL)animated {
self.tabBarController.navigationController.navigationBar.barTintColor
= [UIColor colorWithRed:0.890
green:0.471
blue:0.118
alpha:1.000];
self.tabBarController.navigationController.title =
@"空中夺宝";
}
- (void)viewDidLoad {
[super
viewDidLoad];
web = [[UIWebView
alloc]initWithFrame:CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height -
40)];
[web
setUserInteractionEnabled:YES];//是否支持交互
web.delegate=self;
[web
setOpaque:NO];
//opaque是不透明的意思
[web
setScalesPageToFit:YES];//自动缩放以适应屏幕
[self.view
addSubview:web];
NSURL *url = [NSURL
URLWithString:@"http://www.baidu.com"];
[web
loadRequest:[NSURLRequest
requestWithURL:url]];
//2.加载本地文件资源
/* NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];*/
//3.读入一个HTML,直接写入一个HTML代码
//NSString *htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/loadar.html"];
//NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL];
//[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
opaqueView = [[UIView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
activityIndicatorView = [[UIActivityIndicatorView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
[activityIndicatorView
setCenter:opaqueView.center];
[activityIndicatorView
setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[opaqueView
setBackgroundColor:[UIColor
blackColor]];
[opaqueView
setAlpha:0.6];
[self.view
addSubview:opaqueView];
[opaqueView
addSubview:activityIndicatorView];
// Do any additional setup after loading the view.
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
[activityIndicatorView
startAnimating];
opaqueView.hidden =
NO;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[activityIndicatorView
startAnimating];
opaqueView.hidden =
YES;
}
//UIWebView如何判断 HTTP 404
等错误
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSURL *url = [NSURL
URLWithString:@"http://www.baidu.com"];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ((([httpResponse
statusCode]/100) ==
2)) {
// self.earthquakeData = [NSMutableData data];
[UIApplication
sharedApplication].networkActivityIndicatorVisible =
YES;
[ web
loadRequest:[ NSURLRequest
requestWithURL: url]];
web.delegate =
self;
} else {
NSDictionary *userInfo = [NSDictionary
dictionaryWithObject:
NSLocalizedString(@"HTTP Error",
@"Error message displayed when receving a connection error.")
forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError
errorWithDomain:@"HTTP"
code:[httpResponse
statusCode] userInfo:userInfo];
if ([error
code] == 404) {
// NSLog(@"xx");
web.hidden =
YES;
}
}
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
运行吧,是不是成功了!~~
#import <UIKit/UIKit.h>
#import "FirstView.h"
@interface FirstViewController :
UIViewController<UIWebViewDelegate> {
UIWebView *web;
UIActivityIndicatorView *activityIndicatorView;
UIView *opaqueView;
}
@property (nonatomic,
strong) FirstView *fv;
@end
在.m中,如下:
#import "FirstViewController.h"
@interface
FirstViewController ()
@end
@implementation FirstViewController
- (void)loadView {
[super loadView];
self.fv = [[FirstView
alloc] initWithFrame:[UIScreen
mainScreen].bounds];
self.view =
self.fv;
}
- (void)viewWillAppear:(BOOL)animated {
self.tabBarController.navigationController.navigationBar.barTintColor
= [UIColor colorWithRed:0.890
green:0.471
blue:0.118
alpha:1.000];
self.tabBarController.navigationController.title =
@"空中夺宝";
}
- (void)viewDidLoad {
[super
viewDidLoad];
web = [[UIWebView
alloc]initWithFrame:CGRectMake(0,
0, self.view.frame.size.width,
self.view.frame.size.height -
40)];
[web
setUserInteractionEnabled:YES];//是否支持交互
web.delegate=self;
[web
setOpaque:NO];
//opaque是不透明的意思
[web
setScalesPageToFit:YES];//自动缩放以适应屏幕
[self.view
addSubview:web];
NSURL *url = [NSURL
URLWithString:@"http://www.baidu.com"];
[web
loadRequest:[NSURLRequest
requestWithURL:url]];
//2.加载本地文件资源
/* NSURL *url = [NSURL fileURLWithPath:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];*/
//3.读入一个HTML,直接写入一个HTML代码
//NSString *htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/loadar.html"];
//NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL];
//[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
opaqueView = [[UIView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
activityIndicatorView = [[UIActivityIndicatorView
alloc]initWithFrame:[UIScreen
mainScreen].bounds];
[activityIndicatorView
setCenter:opaqueView.center];
[activityIndicatorView
setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
[opaqueView
setBackgroundColor:[UIColor
blackColor]];
[opaqueView
setAlpha:0.6];
[self.view
addSubview:opaqueView];
[opaqueView
addSubview:activityIndicatorView];
// Do any additional setup after loading the view.
}
-(void)webViewDidStartLoad:(UIWebView *)webView{
[activityIndicatorView
startAnimating];
opaqueView.hidden =
NO;
}
-(void)webViewDidFinishLoad:(UIWebView *)webView{
[activityIndicatorView
startAnimating];
opaqueView.hidden =
YES;
}
//UIWebView如何判断 HTTP 404
等错误
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSURL *url = [NSURL
URLWithString:@"http://www.baidu.com"];
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if ((([httpResponse
statusCode]/100) ==
2)) {
// self.earthquakeData = [NSMutableData data];
[UIApplication
sharedApplication].networkActivityIndicatorVisible =
YES;
[ web
loadRequest:[ NSURLRequest
requestWithURL: url]];
web.delegate =
self;
} else {
NSDictionary *userInfo = [NSDictionary
dictionaryWithObject:
NSLocalizedString(@"HTTP Error",
@"Error message displayed when receving a connection error.")
forKey:NSLocalizedDescriptionKey];
NSError *error = [NSError
errorWithDomain:@"HTTP"
code:[httpResponse
statusCode] userInfo:userInfo];
if ([error
code] == 404) {
// NSLog(@"xx");
web.hidden =
YES;
}
}
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning {
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
运行吧,是不是成功了!~~
相关文章推荐
- iOS获取手机内部音乐列表
- Android消息传递机制----Handler
- iOS各种调试技巧
- [置顶] Android开发之pdf
- Objective-C Runtime 运行时之六:拾遗
- java.lang.ClassCastException: android.widget.*Layout$LayoutParams
- Objective-C Runtime 运行时之五:协议与分类
- Object 类的比较
- android studio 包依赖
- swift学习笔记(一)基本数据类型(下)
- ios 命令行打印视图结构命令
- Objective-C Runtime 运行时之三:方法与消息
- call()、apply()。
- Android线程配合handler详解
- ANDROID源码编译3 - 编译和运行
- Android通过包名打开app
- Android开发:Android studio 无法在可视化页面预览XML布局文件以及丢失R文件
- Android蓝牙开发浅谈
- Android getResources()方法
- Android 耳机状态监听