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的更多相关文章
随机推荐
- 查看JSP和Servlet版本+
如何查看JSP和Servlet版本 找到jsp-api.jar和servlet-api.jar ,分别打开META-INF下的MAINMEFT.MF文件,查看对应的版本. 例: JSP版本: Mani ...
- iptables---linux防火墙
iptables命令是Linux上常用的防火墙软件,是netfilter项目的一部分.可以直接配置,也可以通过许多前端和图形界面配置. 语法 iptables(选项)(参数) 选项 -t<表&g ...
- docker部署mysql 实现远程连接
1. docker search mysql # 查看mysql版本 2. docker pull mysql:5.7 # 拉取mysql 5.7 3. docker images # 查 ...
- C# wpf程序获取当前程序版本
C# wpf程序获取当前程序版本 /// <summary> /// 获取当前系统的版本 /// </summary> /// ...
- worktools-源码下拉问题
今天下拉源码的时候,出现了一个问题,就是当地的内容跟仓库的内容冲突,导致merge冲突.这时候CC指令不能用.然后希望通过checkout到其他分支,然后cc掉的.结果没办法切换到其他分支,一直停留在 ...
- 求第k大的数(用到快速排序算法的思想)
//下面两种part效率比较:相同运算量下part比part2快5倍左右,part2写法简单但是效率低 #include "stdafx.h" #include <iostr ...
- 3/19 Django框架 url路由配置及模板渲染
3/19 Django框架 url路由配置及模板渲染 1.路由分配 URL(Uniform Resoure Locato):统一资源定位符是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示, ...
- Shiro学习总结(3)——Apache Shiro身份认证
身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...
- android插件式开发资料整理
1.DL : Apk动态载入框架 2.android中的动态载入机制
- UVA 11557 - Code Theft (KMP + HASH)
UVA 11557 - Code Theft 题目链接 题意:给定一些代码文本.然后在给定一个现有文本,找出这个现有文本和前面代码文本,反复连续行最多的这些文本 思路:把每一行hash成一个值.然后对 ...