UIWebView / NSURL / NSBoundle 相关应用 (实例,加载完成前的背景, 默认safari打开链接地址等)

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];//创建NSURLRequest  
[webView loadRequest:request];//加载  

 

 也可以加载一个本地资源:

 

NSURL* url = [NSURL   fileURLWithPath:filePath];//创建URL  
NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建NSURLRequest  
[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的委托:

webView.delegate = self;  

 

七、三个方法

- (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;//当网页视图被指示载入内容而得到通知。应当返回YES,这样会进行加载。通过导航类型参数可以得到请求发起的原因,可以是以下任意值:  
UIWebViewNavigationTypeLinkClicked  
UIWebViewNavigationTypeFormSubmitted  
UIWebViewNavigationTypeBackForward  
UIWebViewNavigationTypeReload  
UIWebViewNavigationTypeFormResubmitted  
UIWebViewNavigationTypeOther 

 

-(void)webViewDidStartLoad:(UIWebView*)webView ;//当网页视图已经开始加载一个请求后,得到通知。
  
-(void)webViewDidFinishLoad:(UIWebView*)webView ;//当网页视图结束加载一个请求之后,得到通知。

 

-(void)webView:(UIWebView*)webView  DidFailLoadWithError:(NSError*)error;//当在请求加载中发生错误时,得到通知。会提供一个NSSError对象,以标识所发生错误类型。

 

 

来源: http://blog.csdn.net/iukey/article/details/7299763

 

 

实例:

 

显示图片

CGRect myImage = CGRectMake(10, 10, 140, 100);      //定义坐标和大小
UIImageView *myimageView = [[UIImageView alloc] initWithFrame:myImage];  //初始化UIImageView

[myimageView setImage:[UIImage imageNamed:@"iphonewebsnsxiao.png"]];   //设置图片
myimageView.opaque = YES;   //不透明类型

[window addSubview:myimageView];  //添加到window里
[self.window makeKeyAndVisible];
 

Web view

CGRect webFrame = CGRectMake(0.0f, 0.0f, 320.0f, 460.0f);     //定义坐标和大小
UIWebView *webView = [[UIWebView alloc] initWithFrame:webFrame];//初始化UIWebView

NSString *urlAddress = @"http://www.baidu.com";       //定义一个网址字符串
NSURL *url = [NSURL URLWithString:urlAddress];     //定义NSURL的值
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];  //创建一个返回值

[webView loadRequest:requestObj];     //链接到URL
[window addSubview:webView];         //添加到window里

 

或者 (Empty Application

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    
    
    NSLog(@"loading");
    
    UIView *contentView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    contentView.backgroundColor = [UIColor blueColor];
    
    NSLog(@"self view");
    
    // view orientation rotation
    contentView.autoresizingMask = (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);
    
    //设置属性
    
    //自动调整视图大小
    contentView.autoresizesSubviews = NO;
    [self.window addSubview:contentView];
    
    
    //创建一个层用来放webview  
    UIWebView *aWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
    
    //缩放
    aWebView.scalesPageToFit = NO;
    
    //自动调整大小
    aWebView.autoresizesSubviews = NO;
    
    aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
    //[aWebView setDelegate:self];
    
    NSURL *aURL = [NSURL URLWithString:@"http://www.youtube.com"];
    NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
    
    //发送请求
    [aWebView loadRequest:aRequest];
    
    //把webview添加到内容视图
    
    [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;//背景不透明设置为NO
[self.view addSubview:wv];
self.view.backgroundColor = [UIColor  orangeColor];//其实这里我是为了设置为图片背景,偷懒了,不写了。

 

或者

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    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.webView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"webmap320x640.png"]]];
    [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
 

 

 

 

你可能感兴趣的:(UIWebView)