iOS开发中WebView的使用
来源:互联网 发布:sql注入语句 编辑:程序博客网 时间:2024/04/29 01:11
麻城创捷信息技术有限公司
专业开发App、微信,网站
自http://www.tuicool.com/articles/nAVJ7n
iOS开发中WebView的使用
iOS开发中WebView的使用
在AppDelegate.m文件中
#import "AppDelegate.h"#import "webTableViewController.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; webTableViewController *web = [[webTableViewController alloc]init]; self.window.rootViewController = web; [self.window makeKeyAndVisible]; return YES;}
新键一个类命名为 webTableViewController
webTableViewController.h
#import <UIKit/UIKit.h>@interface webTableViewController : UIViewController<UIWebViewDelegate>{ IBOutlet UIWebView *webView; UIActivityIndicatorView *activityIndicatorView; UIView *opaqueView;}@endwebTableViewController.m
#import "webTableViewController.h"@interface webTableViewController ()@end@implementation webTableViewController- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{ self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self;}- (void)viewDidLoad{ [super viewDidLoad]; webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 44, 320, 440)]; [webView setUserInteractionEnabled:YES];//是否支持交互 //[webView setDelegate:self]; webView.delegate=self; [webView setOpaque:NO];//opaque是不透明的意思 [webView setScalesPageToFit:YES];//自动缩放以适应屏幕 [self.view addSubview:webView]; //加载网页的方式 //1.创建并加载远程网页 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; [webView loadRequest:[NSURLRequest requestWithURL:url]]; //2.加载本地文件资源 /* NSURL *url = [NSURL fileURLWithPath:filePath]; NSURLRequest *request = [NSURLRequest requestWithURL:url]; [webView loadRequest:request];*/ //3.读入一个HTML,直接写入一个HTML代码 //NSString *htmlPath = [[[NSBundle mainBundle]bundlePath]stringByAppendingString:@"webapp/loadar.html"]; //NSString *htmlString = [NSString stringWithContentsOfURL:htmlPath encoding:NSUTF8StringEncoding error:NULL]; //[webView loadHTMLString:htmlString baseURL:[NSURL fileURLWithPath:htmlPath]]; opaqueView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; activityIndicatorView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)]; [activityIndicatorView setCenter:opaqueView.center]; [activityIndicatorView setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite]; [opaqueView setBackgroundColor:[UIColor blackColor]]; [opaqueView setAlpha:0.6]; [self.view addSubview:opaqueView]; [opaqueView addSubview:activityIndicatorView]; }-(void)webViewDidStartLoad:(UIWebView *)webView{ [activityIndicatorView startAnimating]; opaqueView.hidden = NO;}-(void)webViewDidFinishLoad:(UIWebView *)webView{ [activityIndicatorView startAnimating]; opaqueView.hidden = YES;}//UIWebView如何判断 HTTP 404 等错误-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; if ((([httpResponse statusCode]/100) == 2)) { // self.earthquakeData = [NSMutableData data]; [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; [ webView loadRequest:[ NSURLRequest requestWithURL: url]]; webView.delegate = self; } else { NSDictionary *userInfo = [NSDictionary dictionaryWithObject: NSLocalizedString(@"HTTP Error", @"Error message displayed when receving a connection error.") forKey:NSLocalizedDescriptionKey]; NSError *error = [NSError errorWithDomain:@"HTTP" code:[httpResponse statusCode] userInfo:userInfo]; if ([error code] == 404) { NSLog(@"xx"); webView.hidden = YES; } }}- (void)didReceiveMemoryWarning{ [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}@end
点击下面链接进入官网
麻城创捷信息技术有限公司
专业开发App、微信,网站
0 0
- iOS开发中WebView的使用
- iOS开发中WebView的使用
- IOS中WebView的使用
- IOS中WebView的使用
- iOS 中webView的使用
- IOS开发之WebView的使用
- IOS 中WebView的基本使用
- iOS app中使用webview的时机
- iOS中webView的简单实使用
- android开发中WebView的使用
- iOS开发中带 Search 框与前进、后退、刷新、停止 的 WebView 的使用
- ios开发入门- WebView使用
- IOS开发获取webView中网页的信息
- iOS开发获取webview中网页的信息
- [IOS开发]获取webView中网页的信息
- iOS开发中WebView的基本使用方法简介
- (iOS开发)webView的使用与注意事项
- 【IOS开发】webView的用法
- Android APK系列5-------修改APK中的内容
- android 9png的效果a
- Android APK系列6-------APK反编译
- 算法八皇后问题
- VirtualBox双机调试
- iOS开发中WebView的使用
- [POJ 2516]Minimum Cost[费用流][建图]
- jquery ajax级联二级下拉,jquey select级联
- POJ 1035 Spell checker(字符串比较)
- 「C++ Primer」3.22 迭代器未定义操作导致段错误问题
- mysql5.6配置版
- TCP的三次握手(建立连接)和四次挥手(关闭连接)
- Eclipse各种快捷键方式
- JVM