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的更多相关文章
随机推荐
- 【Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined) D】Bash and a Tough Math Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 对于1操作 令len = r-l+1 等价于查找l..r这个范围内x的倍数的个数是否大于等于len-1 也即l..r这个范围内不是x ...
- Java中join和yield的作用
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 1. A.join,在API中的解释是,堵塞当前线程B,直到A执行完毕并死掉,再执行B. 用一个 ...
- iOS -读书笔记-网络请求
知道"3次握手"吗?突然想起这个词 什么是3次握手? TCP三次握手/四次挥手详解 这里是3次握手的详解 3次握手就是为了可靠的传送数据,TCP(什么是TCP呢?TCP就是一种可靠 ...
- c、c++ 结构体的嵌套
c.c++ 结构体的嵌套 /************************************************************************/ /* 嵌套结构体 * C ...
- 安卓使用WebView下载文件,安卓实现软件升级功能
由于调用系统默认浏览器下载更新,造成用户体验非常不好,所以决定在webview中直接下载系统更新.然后直接安装. 由于要下载,所以必须用webview,联网权限这里不说了,直接写在manifafest ...
- 类名引用static变量好处
不仅强调了变量static的结构,而且在有些情况下他还为编译器进行优化提供了更好的机会.
- 1.5 Upgrading From Previous Versions官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 1.5 Upgrading From Previous Versions 1.5 从 ...
- 洛谷 P1100 高低位交换
P1100 高低位交换 题目描述 给出一个小于2^32的正整数.这个数可以用一个32位的二进制数表示(不足32位用0补足).我们称这个二进制数的前16位为“高位”,后16位为“低位”.将它的高低位交换 ...
- linux又一次编译安装gd,添加freetype支持,解决验证码不显示问题,Fatal error: Call to undefined function imagettftext()
问题: Fatal error: Call to undefined function Think\imagettftext() in /var/www/webreg/ThinkPHP/Library ...
- 【SonicUI】关于字体高亮的问题。。
m_pSonicString[1]->Format(_T("/c=%x, a='http://hi.csdn.net/', linkh=0xFF00F0, font, font_hei ...