ios_webView
iOS开发中WebView的使用
在AppDelegate.m文件里
sourceprint
" class="item about" style="color:rgb(51,51,51); text-decoration:none; margin:0px 0px 0px 8px; padding:0px; border-width:0px 0px 1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51); outline:0px; float:left; vertical-align:baseline; position:static; left:auto; top:auto; right:auto; bottom:auto; height:16px; width:16px; display:block; overflow:hidden; text-indent:-5000px">?
01.
#
import
"AppDelegate.h"
02.
#
import
"webTableViewController.h"
03.
@implementation
AppDelegate
04.
05.
-
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
06.
{
07.
self.window
= [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
08.
//
Override point for customization after application launch.
09.
self.window.backgroundColor
= [UIColor whiteColor];
10.
webTableViewController
*web = [[webTableViewController alloc]init];
11.
self.window.rootViewController
= web;
12.
[self.window
makeKeyAndVisible];
13.
return
YES;
14.
}
新键一个类命名为webTableViewController
webTableViewController.h
sourceprint
" class="item about" style="color:rgb(51,51,51); text-decoration:none; margin:0px 0px 0px 8px; padding:0px; border-width:0px 0px 1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51); outline:0px; float:left; vertical-align:baseline; position:static; left:auto; top:auto; right:auto; bottom:auto; height:16px; width:16px; display:block; overflow:hidden; text-indent:-5000px">?
01.
#
import
<UIKit/UIKit.h>
02.
03.
@interface
webTableViewController
: UIViewController<UIWebViewDelegate>
04.
{
05.
IBOutlet
UIWebView *webView;
06.
07.
UIActivityIndicatorView
*activityIndicatorView;
08.
UIView
*opaqueView;
09.
}
10.
11.
@end
webTableViewController.m
sourceprint
" class="item about" style="color:rgb(51,51,51); text-decoration:none; margin:0px 0px 0px 8px; padding:0px; border-width:0px 0px 1px; border-bottom-style:dotted; border-bottom-color:rgb(51,51,51); outline:0px; float:left; vertical-align:baseline; position:static; left:auto; top:auto; right:auto; bottom:auto; height:16px; width:16px; display:block; overflow:hidden; text-indent:-5000px">?
01.
#
import
"webTableViewController.h"
02.
03.
@interface
webTableViewController
()
04.
05.
@end
06.
07.
@implementation
webTableViewController
08.
-
(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
09.
{
10.
self
= [
super
initWithNibName:nibNameOrNil
bundle:nibBundleOrNil];
11.
if
(self)
{
12.
//
Custom initialization
13.
}
14.
return
self;
15.
}
16.
17.
-
(
void
)viewDidLoad
18.
{
19.
[
super
viewDidLoad];
20.
webView
= [[UIWebView alloc]initWithFrame:CGRectMake(
0
,
44
,
320
,
440
)];
21.
[webView
setUserInteractionEnabled:YES];
//是否支持交互
22.
//[webView
setDelegate:self];
23.
webView.delegate=self;
24.
[webView
setOpaque:NO];
//opaque是不透明的意思
25.
[webView
setScalesPageToFit:YES];
//自己主动缩放以适应屏幕
26.
[self.view
addSubview:webView];
27.
28.
//载入网页的方式
29.
//1.创建并载入远程网页
31.
[webView
loadRequest:[NSURLRequest requestWithURL:url]];
32.
//2.载入本地文件资源
33.
/*
NSURL *url = [NSURL fileURLWithPath:filePath];
34.
NSURLRequest
*request = [NSURLRequest requestWithURL:url];
35.
[webView
loadRequest:request];*/
36.
//3.读入一个HTML。直接写入一个HTML代码
37.
//NSString
*htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/test.html"];
38.
//NSString
*htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL];
39.
//[webView
loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]];
40.
41.
opaqueView
= [[UIView alloc]initWithFrame:CGRectMake(
0
,
0
,
320
,
480
)];
42.
activityIndicatorView
= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(
0
,
0
,
320
,
480
)];
43.
[activityIndicatorView
setCenter:opaqueView.center];
44.
[activityIndicatorView
setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];
45.
[opaqueView
setBackgroundColor:[UIColor blackColor]];
46.
[opaqueView
setAlpha:
0.6
];
47.
[self.view
addSubview:opaqueView];
48.
[opaqueView
addSubview:activityIndicatorView];
49.
50.
51.
}
52.
53.
-(
void
)webViewDidStartLoad:(UIWebView
*)webView{
54.
[activityIndicatorView
startAnimating];
55.
opaqueView.hidden
= NO;
56.
}
57.
58.
-(
void
)webViewDidFinishLoad:(UIWebView
*)webView{
59.
[activityIndicatorView
startAnimating];
60.
opaqueView.hidden
= YES;
61.
}
62.
63.
//UIWebView怎样推断
HTTP 404 等错误
64.
-(
void
)connection:(NSURLConnection
*)connection didReceiveResponse:(NSURLResponse *)response{
66.
NSHTTPURLResponse
*httpResponse = (NSHTTPURLResponse *)response;
67.
if
((([httpResponse
statusCode]/
100
)
==
2
))
{
68.
//
self.earthquakeData = [NSMutableData data];
69.
[UIApplication
sharedApplication].networkActivityIndicatorVisible = YES;
70.
71.
[
webView loadRequest:[ NSURLRequest requestWithURL: url]];
72.
webView.delegate
= self;
73.
}
else
{
74.
NSDictionary
*userInfo = [NSDictionary dictionaryWithObject:
75.
NSLocalizedString(@
"HTTP
Error"
,
76.
@
"Error
message displayed when receving a connection error."
)
77.
forKey:NSLocalizedDescriptionKey];
78.
NSError
*error = [NSError errorWithDomain:@
"HTTP"
code:[httpResponse
statusCode] userInfo:userInfo];
79.
80.
if
([error
code] ==
404
)
{
81.
NSLog(@
"xx"
);
82.
webView.hidden
= YES;
83.
}
84.
85.
}
86.
}
87.
-
(
void
)didReceiveMemoryWarning
88.
{
89.
[
super
didReceiveMemoryWarning];
90.
//
Dispose of any resources that can be recreated.
91.
}
92.
93.
@end
ios_webView的更多相关文章
随机推荐
- JavaScript--数据结构与算法之链表实现约瑟夫环
传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第三个人时将 ...
- 小米开源文件管理器MiCodeFileExplorer-源码研究(7)-Favorite收藏管理和SQLite数据库CRUD
FavoriteDatabaseHelper,存储favorite数据,到SQLite数据库.SQLiteOpenHelper是一个帮助管理数据库和版本的工具类.通过继承并重载方法,快速实现了我们自己 ...
- JNDI学习总结(3)——Tomcat下使用C3P0配置JNDI数据源
一.C3P0下载 C3P0下载地址:http://sourceforge.net/projects/c3p0/files/?source=navbar 下载完成之后得到一个压缩包. 二.使用C3P0配 ...
- HDU——T 4738 Caocao's Bridges
http://acm.hdu.edu.cn/showproblem.php?pid=4738 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- Tomcat之——配置项目有虚拟路径
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47024863 非常easy,在Tomcat的Server.xml文件里的Host节 ...
- javascript创建对象的方法--原型模式
javascript创建对象的方法--原型模式 一.总结 1.原型模式解决内存浪费的方法(继承):通过继承,对象继承原型模式下的所有属性,对象不同于其它对象的的属性自己创建或者修改 2.原型的使用(p ...
- 1.17 Python基础知识 - 迭代器和生成器初识
可循环迭代的对象称为可迭代对象,迭代器和生成器函数是可迭代对象. 列表解析表达式:可以简单高效处理一个可迭代对象,并生成结果列表 示例代码: [ i ** 2 for i in range(10) ] ...
- jmeter实现分布式压测步骤
环境说明:安装与控制机相同版本的jdk与jmeter 1.修改控制机中的jmeter.properties文件 将<remote_hosts=127.0.0.1>改为<remote_ ...
- AutoCAD 出现“安全系统(软件锁许可管理器)不起作用或未正确安装”的解决方法
感谢高飞鸟提供解决方案.当AutoCAD或自动桌子公司的其它产品在启动过程中突然停电或其它原因造成操作系统重启时,可能会造成这些产品的许可出错而无法再运行.一般出错后第一次进入时,会提示你“产品需要激 ...
- 有趣的Ruby-学习笔记4
Ruby块 块.在我看来就是插入一段可变的函数 block_name{ statement1 statement2 .......... } 看起来不知道是什么,只是别急,继续往下看. 块函数通过yi ...