iOS开发中WebView的使用

在AppDelegate.m文件里

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

webTableViewController.m

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(
044320440)];
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.创建并载入远程网页
30.NSURL
*url = [NSURL URLWithString:@
"http://www.csdn.com"];
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(
00320480)];
42.activityIndicatorView
= [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(
00320480)];
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{
65.NSURL
*url = [NSURL URLWithString:@
"http://www.csdn.com"];
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的更多相关文章

随机推荐

  1. kali之Nmap (Network Mapper(网络映射器)

    Nmap是主机扫描工具,他的图形化界面是Zenmap,分布式框架为Dnamp. Nmap可以完成以下任务: 主机探测 端口扫描 版本检测 系统检测 支持探测脚本的编写 Nmap在实际中应用场合如下: ...

  2. centos6.5 数据库的安装

    mongo https://www.cnblogs.com/layezi/p/7290082.html

  3. 洛谷 P2837 晚餐队列安排

    P2837 晚餐队列安排 题目背景 Usaco Feb08 Bronze 题目描述 为了避免餐厅过分拥挤,FJ要求奶牛们分2批就餐.每天晚饭前,奶牛们都会在餐厅前排队入内,按FJ的设想,所有第2批就餐 ...

  4. Tomcat之——配置项目有虚拟路径

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47024863 非常easy,在Tomcat的Server.xml文件里的Host节 ...

  5. Rsync 指令的使用方法

    RsyncLinux版下载:http://rsync.samba.org/download.htmlWindows版下载:https://www.itefix.no/i2/cwrsync-get 选( ...

  6. C#之使用app.config可记录数据,下次打开可读取记录的数据

    一.背景 如下图所示,我通过open..按键打开了某个文件,之后我再把app给关闭掉,当再次打开app的时候,在textBox.Text上显示上一次打开的文件路径.通过使用app.config可以保存 ...

  7. STL中erase()的陷阱

    最近在刷stl源码剖析这本书时,对于vector的erase()函数引起了我的注意 在删除单个元素时是这样定义的: iterator erase(iterator position){ !=end() ...

  8. ArcGIS Engine 线段绘制

    转自ArcGIS Engine 线段绘制研究 基本步骤 构建形状 1. 创建 IPoint IPoint m_Point = new PointClass(); m_Point.PutCoords(x ...

  9. Spring.net的Demo项目,了解什么是控制反转

    Spring这个思想,已经推出很多年了. 刚开始的时候,首先是在Java里面提出,后来也推出了.net的版本. Spring里面最主要的就是控制反转(IOC)和依赖注入(DI)这两个概念. 网上很多教 ...

  10. ArcEngine的ToolbarControl解析

    转自Love Lyre原文 ArcEngine的ToolbarControl解析 ToolbarControlClass有三个主要的接口: IToolbarControl, IToolbarContr ...