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
最新文章
- 免费开源的 .NET 分布式组件库 Exceptionless Foundatio
- java实现二叉树
- iOS 面试题(四):block 什么时候需要构造循环引用 --转自唐巧
- Berkeley DB的数据存储结构——哈希表(Hash Table)、B树(BTree)、队列(Queue)、记录号(Recno)
- ZBrush中怎样对遮罩进行反选
- Eclipse运行内存溢出
- 使用Freemarker宏进行可扩展式模块化编程
- 【BZOJ】2463: [中山市选2009]谁能赢呢?(博弈论)
- Android笔记:C memory copy
- Linux&;shell之高级Shell脚本编程-创建菜单
- 在ubuntu上安装pyenv出现的问题
- Linux显示指定区块大小为1048576字节
- LeetCode 13. Roman to Integer(c语言版)
- lnmp环境一些基本命令行
- 大数据小白系列——HDFS(1)
- day053 url反向解析图解 模板渲染
- 【错误记录】PowerShell 超级无语的语法错误(令人怀疑人生)
- Mongodb集群节点故障恢复场景分析(转)
- 深入理解多线程(二)—— Java的对象模型
- 第十九章,指针小练习(C++)
热门文章
- BZOJ1835: [ZJOI2010]base 基站选址(线段树优化Dp)
- 【Django】MEDIA的配置及用法
- 【例题 8-13 UVA - 11093】Just Finish it up
- 【Codeforces Round #426 (Div. 2) B】The Festive Evening
- WinRAR 5.40无弹窗广告注册版下载
- windows 静态和动态库
- vue中类名和组件经过刷新不对应的解决办法
- BZOJ3926: [Zjoi2015]诸神眷顾的幻想乡(广义后缀自动机)
- Direct2D 图形计算
- 【Codeforces Round #453 (Div. 2) A】 Visiting a Friend