ios_webView

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

 

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

 

 

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值