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. linux操作指令:

    系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS ...

  2. Install Docker Mac OS X

    检查 Mac OS version 要求必须是 OS X 10.6 Snow Leopard or newer to run Boot2Docker 安装 Boot2Docker 列表内容 下载地址: ...

  3. java JDK设置环境变量

    1.右键"我的电脑"图标.在弹出菜单中依次选择"属性"-"高级"-"环境变量". 2.在"环境变量" ...

  4. Scala具体解释---------类

    Scala中的类 摘要: 在本篇中.你将会学习怎样用Scala实现类. 假设你了解Java或C++中的类,你不会认为这有多难.而且你会非常享受Scala更加精简的表示法带来的便利.本篇的要点包含: 1 ...

  5. Qt使用第三方库

    简述 在 Qt 中经常会用到第三方库,例如:FFmpeg.OpenCV 等.第三方库的使用比较简单,只需要一些基本的配置就可以搞定,一起来看看吧! 简述 第三方库 源代码 库文件 目标目录 第三方库 ...

  6. HTML5手机应用的最大优势就是可以在网页上直接调试和修改

    HTML5手机应用的最大优势就是可以在网页上直接调试和修改

  7. ByteUtils

    package sort.bing.com; import java.io.ByteArrayOutputStream;import java.io.DataOutputStream;import j ...

  8. Fragment 实现的 分类 效果

    Fragment   实现的 分类 效果 布局文件的信息: <LinearLayout xmlns:android="http://schemas.android.com/apk/re ...

  9. Activity启动模式的深入分析

    网上关于Activity启动模式的文章许多.可是看起来都千篇一律,看完之后我们都能理解这4种启动模式.只是官方api对singleTask这个启动模式解释有些争议,导致我事实上并没有真正理解这几种模式 ...

  10. 如何去掉bootstrap table中表格样式中横线竖线

    修改之前,表格看上去比较拥挤,因为bootstrap table插件中自带斑马线表格样式,有横线和竖线分栏,现在我们不需要这些. 应UI设计的要求,要去掉中间的横线和竖线,使用了修改需求中一种简单粗暴 ...