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;}@end
webTableViewController.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