UIWebView可以让你创建一个网页浏览器,类似safari,而不是在程序中启动safsri哦。是不是觉得很棒呢?废话少说,切入正题。
一、创建UIWebView
- CGRect bouds = [[UIScreen mainScreen]applicationFrame];
- UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
二、设置属性
- webView.scalespageToFit = YES;
- webView.detectsPhoneNumbers = YES;
- webView.autoresizesSubviews = NO;
- webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
三、显示网页视图UIWebView
- [self.view addSubview:webView];
四、加载内容
- NSURL* url = [NSURL URLWithString:@"http://www.youku.com"];//创建URL
- NSURLRequest* request = [NSURLRequest requestWithURL:url];
- [webView loadRequest:request];
也可以加载一个本地资源:
- NSURL* url = [NSURL fileURLWithPath:filePath];
- NSURLRequest* request = [NSURLRequest requestWithURL:url];
- [webView loadRequest:request];
UIWebView 还支持将一个NSString对象作为源来加载。你可以为其提供一个基础URL,来指导UIWebView对象如何跟随链接和加载远程资源:
- [webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
五、导航
UIWebView类内部会管理浏览器的导航动作,通过goForward和goBack方法你可以控制前进与后退动作:
- [webView goBack];
- [webView goForward];
- [webView reload];
- [webView stopLoading];
六、UIWebViewDelegate委托代理
UIWebView支持一组委托方法,这些方法将在特定时间得到通知。要使用这些方法,必须先设定webView的委托:
七、三个方法
- - (void)loadRequest:(NSURLRequest *)request;
- - (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL;
- - (void)loadData:(NSData *)data MIMEType:(NSString *)MIMEType textEncodingName:(NSString *)textEncodingName baseURL:(NSURL *)baseURL;
其中baseURL 是指基准的url 是一个绝对的地址,程序要用到的其他资源就可以根据这个基准地址进行查找而不用再次定位到绝对地址;
下面每个委托方法的第一个参数都是指向一个UIwebview的指针,因此你可以将一个委托用于多个网页视图。
- -(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType:(UIWebViewNavigationType)navigationType;
- UIWebViewNavigationTypeLinkClicked
- UIWebViewNavigationTypeFormSubmitted
- UIWebViewNavigationTypeBackForward
- UIWebViewNavigationTypeReload
- UIWebViewNavigationTypeFormResubmitted
- UIWebViewNavigationTypeOther
- -(void)webViewDidStartLoad:(UIWebView*)webView ;
- -(void)webViewDidFinishLoad:(UIWebView*)webView ;
- -(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;
实例:
显示图片
- CGRect myImage = CGRectMake(10, 10, 140, 100);
- UIImageView *myimageView = [[UIImageView alloc] initWithFrame:myImage];
-
- [myimageView setImage:[UIImage imageNamed:@"iphonewebsnsxiao.png"]];
- myimageView.opaque = YES;
-
- [window addSubview:myimageView];
- [self.window makeKeyAndVisible];
Web view
- CGRect webFrame = CGRectMake(0.0f, 0.0f, 320.0f, 460.0f);
- UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];
-
- NSString *urlAddress = @"http://www.baidu.com"; //定义一个网址字符串
- NSURL *url = [NSURL URLWithString:urlAddress];
- NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
-
- [webView loadRequest:requestObj];
- [window addSubview:webView];
或者 (Empty Application )
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
-
-
-
- NSLog(@"loading");
-
- UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
- contentView.backgroundColor = [UIColor blueColor];
-
- NSLog(@"self view");
-
-
- contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
-
-
-
-
- contentView.autoresizesSubviews = NO;
- [self.window addSubview:contentView];
-
-
-
- UIWebView *aWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
-
-
- aWebView.scalesPageToFit = NO;
-
-
- aWebView.autoresizesSubviews = NO;
-
- aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
-
-
- NSURL *aURL = [NSURL URLWithString:@"http://www.youtube.com"];
- NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
-
-
- [aWebView loadRequest:aRequest];
-
-
-
- [contentView addSubview:aWebView];
-
- [self.window makeKeyAndVisible];
-
- aWebView = nil;
- contentView = nil;
- return YES;
- }
UIWebView 加载网页时使用程序中的背景( 解决加载页面时一片空白问题 )
UIWebView加载网页时默认使用了网页中的背景,而不能那使用程序中的主题背景,这让人很不爽。下面给出我的解决办法。
首先我在网页的css中加上了:
- body{
- background-color:transparent;
- }
然后直接看代码:
- UIWebView *wv = [[UIWebView alloc]initWithFrame:CGRectMake(0.0,0.0,320.0,460.0)];
- wv.backgroundColor = [UIColor clearColor];
- wb.opaque = NO;
- [self.view addSubview:wv];
- self.view.backgroundColor = [UIColor orangeColor];
或者
- - (void)viewDidLoad
- {
- [super viewDidLoad];
-
-
- NSURL *url = [NSURL URLWithString:@"http://www.lebunnybleu.com/seoul/storelocation"];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- self.webview.backgroundColor = [UIColor clearColor];
- self.webview.opaque = NO;
-
- [self.webviewsetBackgroundColor:[UIColor redColor]];
-
- [self.webviewloadRequest:request];
- }
UIWebView 加载本地html文件(demo.html)
- CGRect bouds = CGRectMake(0, halfHight, viewBouds.size.width, halfHight);
- UIWebView *webview = [[UIWebView alloc] initWithFrame:bouds];
-
- webview.scalesPageToFit = YES;
- webview.autoresizesSubviews = YES;
- webview.autoresizingMask = (UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
-
- [webview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"demo" ofType:@"html"] isDirectory:NO]]];
-
- [self.view addSubview:webview];
载入html的方法
1.
- NSString *resourcePath = [[NSBundle mainBundle] resourcePath];
- NSString *filePath = [resourcePath stringByAppendingPathComponent:@"webpage.html"];
- NSString *htmlstring=[[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
- [uiwebview loadHTMLString:htmlstring baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
或者
- NSString *str = [NSString stringWithFormat:@"<html><head><style>body{margin:0;padding:0}</style></head><body><iframe marginwidth=0 marginheight=0 frameborder=0 scrolling='no' src='http://tv.ibtimes.com'></iframe></body></html>"];
- [webview loadHTMLString:str baseURL:[NSURL URLWithString:@"http://www.ibtimes.com"]];
2.
- NSString *webpage = [NSBundle pathForResource:@"webpage" ofType:@"html" inDirectory:[[NSBundle mainBundle] bundlePath]];
- [uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:webpage]]];
3.
- [uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://qq.com"]]];
NSBundle的用法
NSBundle的对象可以获取应用程序安装目录的附件。附件包括了,当前应用程序下,所有的文件。(图片、属性列表等)
获取XML文件
- NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"];
- NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
获取TXT文件
- NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"myFile" ofType:@"txt"];
- NSData *data = [NSData dataWithContentsOfFile:filePath];
获取属性列表
- NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]];
默认safari打开链接地址
.h
- #import <UIKit/UIKit.h>
-
- @interface adFullScreen : UIViewController <UIWebViewDelegate>
- {
- IBOutlet UIWebView *webview;
- }
-
- @end
.m
- - (void) viewDidLoad
- {
- NSString *adHTML = @"<html><head><style>body{margin:0;padding:0}</style></head><body><iframe width=1024 height=768 marginwidth=0 marginheight=0 frameborder=0 scrolling='no' src='http://oascentral.ibtimes.com/RealMedia/ads/adstream_sx.ads/ipad.ibtimes/home@Position2'></iframe></body></html>";
-
- [webview loadHTMLString:adHTML baseURL:[NSURL URLWithString:@"http://justcoding.iteye.com"]];
- webview.delegate = self;
-
- adHTML = nil;
- }
-
- -(BOOL) webView:(UIWebView *)inWeb shouldStartLoadWithRequest:(NSURLRequest *)inRequest navigationType:(UIWebViewNavigationType)inType {
- if ( inType == UIWebViewNavigationTypeLinkClicked ) {
- [[UIApplication sharedApplication] openURL:[inRequest URL]];
- return NO;
- }
- return YES;
- }
如果不想设置点击,而是打开后直接跳转一个网址,只要用以下代码来代替
- - (BOOL)webView:(UIWebView *)webView
- shouldStartLoadWithRequest:(NSURLRequest *)request
- navigationType:(UIWebViewNavigationType)navigationType
- {
- if ([[[request URL] absoluteString] isEqual:@"http://justcoding.iteye.com"])
- return YES;
-
- [[UIApplication sharedApplication] openURL:[request URL]];
-
- return NO;
- }
他的其他方法和属性是:
- typedef enum {
- UIWebViewNavigationTypeLinkClicked,
- UIWebViewNavigationTypeFormSubmitted,
- UIWebViewNavigationTypeBackForward,
- UIWebViewNavigationTypeReload,
- UIWebViewNavigationTypeFormResubmitted,
- UIWebViewNavigationTypeOther
- } UIWebViewNavigationType;
- @protocol UIWebViewDelegate <NSObject>
-
- @optional
- - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType;
- - (void)webViewDidStartLoad:(UIWebView *)webView;
- - (void)webViewDidFinishLoad:(UIWebView *)webView;
- - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error;
-
- @end