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. Codeforces Round #196 (Div. 2) 少部分题解

    A:sort以后求差值最小 ]; int main() { int n,m; cin>>n>>m; ; i < m ; i++) cin>>a[i]; sor ...

  3. IOS越狱开发错误解决

      Questions: haseScriptExecution Run\ Script /Users/jun/Library/Developer/Xcode/DerivedData/ButtonMa ...

  4. [React] Use the URL as the source of truth in React

    In Single Page Apps we're used to fetch the data on event callbacks. That disables the capacity to u ...

  5. 推断一个java文件和邮箱格式是否合法

    import java.util.Scanner; public class StringTest { public static void main(String[] args) { int bac ...

  6. python 新模块或者包的安装方法

    主要介绍通过pip自动工具来安装需要的包. 1,先安装pip 下载pip的包(包括setup.py文件) cmd载入到pip本地文件所在路径,使用命令进行安装. python setup.py ins ...

  7. bzoj3786星系探索(splay维护dfs序)

    Description 物理学家小C的研究正遇到某个瓶颈. 他正在研究的是一个星系,这个星系中有n个星球,其中有一个主星球(方便起见我们默认其为1号星球),其余的所有星球均有且仅有一个依赖星球.主星球 ...

  8. Spring3拦截引发的问题——WEB开发中的client路径

    什么是client路径? 第一类.也就是html或js文件等client訪问的文件里的路径,这里包含一些资源文件的引入(js.css还有各种图片等),或是跳转到静态html页面,总之获取的都是静态资源 ...

  9. mysql中配置ssl_key、ssl-cert、ssl-ca的路径及建立ssl连接(适用于5.7以下版本,5.7及以上请看本文末尾的备注)

    1.创建 CA 私钥和 CA 证书 (1)下载并安装openssl,将bin目录配置到环境变量: (2)设置openssl.cfg路径(若不设置会报错,找不到openssl配置文件) \bin\ope ...

  10. $(dom).each(index,ele){} 真正的jquery对象

    1.$(ele)才是each真正的jquery对象,而不是ele.$('.mt-story-info').each(function(index,ele){ if($('.mt-story-info' ...