UIWebView /NSURL / NSBoundle 相關(guān)應(yīng)用 (實例衙吩,加載完成前的背景, 默認safari打開鏈接地址等)
來源:http://justcoding.iteye.com/blog/1455674
UIWebView可以讓你創(chuàng)建一個網(wǎng)頁瀏覽器般渡,類似safari赠摇,而不是在程序中啟動safsri哦。是不是覺得很棒呢锭魔?廢話少說芭挽,切入正題。
一笋除、創(chuàng)建UIWebView
CGRect bouds = [[UIScreen mainScreen]applicationFrame];
UIWebView* webView = [[UIWebView alloc]initWithFrame:bounds];
二、設(shè)置屬性
webView.scalespageToFit = YES;//自動對頁面進行縮放以適應(yīng)屏幕
webView.detectsPhoneNumbers = YES;//自動檢測網(wǎng)頁上的電話號碼炸裆,單擊可以撥打
webView.autoresizesSubviews = NO; //自動調(diào)整大小
webView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
三垃它、顯示網(wǎng)頁視圖UIWebView
[self.view addSubview:webView];
四、加載內(nèi)容
NSURL* url = [NSURL URLWithString:@"http://www.youku.com"];//創(chuàng)建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//創(chuàng)建NSURLRequest
[webView loadRequest:request];//加載
也可以加載一個本地資源:
NSURL* url = [NSURL fileURLWithPath:filePath];//創(chuàng)建URL
NSURLRequest* request = [NSURLRequest requestWithURL:url];//創(chuàng)建NSURLRequest
[webView loadRequest:request];//加載
UIWebView還支持將一個NSString對象作為源來加載晒衩。你可以為其提供一個基礎(chǔ)URL嗤瞎,來指導UIWebView對象如何跟隨鏈接和加載遠程資源:
[webView loadHTMLString:myHTML baseURL:[NSURL URLWithString:@"http://baidu.com"]];
五、導航
UIWebView類內(nèi)部會管理瀏覽器的導航動作听系,通過goForward和goBack方法你可以控制前進與后退動作:
[webView goBack];
[webView goForward];
[webView reload];//重載
[webView stopLoading];//取消載入內(nèi)容
六贝奇、UIWebViewDelegate委托代理
UIWebView支持一組委托方法,這些方法將在特定時間得到通知靠胜。要使用這些方法掉瞳,必須先設(shè)定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是一個絕對的地址浪漠,程序要用到的其他資源就可以根據(jù)這個基準地址進行查找而不用再次定位到絕對地址陕习;
下面每個委托方法的第一個參數(shù)都是指向一個UIwebview的指針,因此你可以將一個委托用于多個網(wǎng)頁視圖址愿。
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*) reuqest navigationType:(UIWebViewNavigationType)navigationType;
//當網(wǎng)頁視圖被指示載入內(nèi)容而得到通知该镣。應(yīng)當返回YES,這樣會進行加載响谓。通過導航類型參數(shù)可以得到請求發(fā)起的原因损合,可以是以下任意值:
UIWebViewNavigationTypeLinkClicked
UIWebViewNavigationTypeFormSubmitted
UIWebViewNavigationTypeBackForward
UIWebViewNavigationTypeReload
UIWebViewNavigationTypeFormResubmitted
UIWebViewNavigationTypeOther
-(void)webViewDidStartLoad:(UIWebView*)webView ;//當網(wǎng)頁視圖已經(jīng)開始加載一個請求后,得到通知娘纷。
-(void)webViewDidFinishLoad:(UIWebView*)webView ;//當網(wǎng)頁視圖結(jié)束加載一個請求之后嫁审,得到通知。
-(void)webView:(UIWebView*)webView DidFailLoadWithError:(NSError*)error;//當在請求加載中發(fā)生錯誤時赖晶,得到通知律适。會提供一個NSSError對象,以標識所發(fā)生錯誤類型。
來源: 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"]]; //設(shè)置圖片
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"; //定義一個網(wǎng)址字符串
NSURL *url = [NSURL URLWithString:urlAddress]; //定義NSURL的值
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; //創(chuàng)建一個返回值
[webView loadRequest:requestObj]; //鏈接到URL
[window addSubview:webView]; //添加到window里
或者 (EmptyApplication )
- (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);
//設(shè)置屬性
//自動調(diào)整視圖大小
contentView.autoresizesSubviews = NO;
[self.window addSubview:contentView];
//創(chuàng)建一個層用來放webview
UIWebView *aWebView = [[UIWebView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//縮放
aWebView.scalesPageToFit = NO;
//自動調(diào)整大小
aWebView.autoresizesSubviews = NO;
aWebView.autoresizingMask=(UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth);
//[aWebView setDelegate:self];
NSURL *aURL = [NSURL URLWithString:@"http://www.youtube.com"];
NSURLRequest *aRequest = [NSURLRequest requestWithURL:aURL];
//發(fā)送請求
[aWebView loadRequest:aRequest];
//把webview添加到內(nèi)容視圖
[contentView addSubview:aWebView];
[self.window makeKeyAndVisible];
aWebView = nil;
contentView = nil;
return YES;
}
UIWebView 加載網(wǎng)頁時使用程序中的背景(解決加載頁面時一片空白問題 )
UIWebView加載網(wǎng)頁時默認使用了網(wǎng)頁中的背景捂贿,而不能那使用程序中的主題背景纠修,這讓人很不爽。下面給出我的解決辦法厂僧。
首先我在網(wǎng)頁的css中加上了:
Html代碼
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;//背景不透明設(shè)置為NO
[self.view addSubview:wv];
self.view.backgroundColor = [UIColor orangeColor];//其實這里我是為了設(shè)置為圖片背景分瘾,偷懶了,不寫了吁系。
或者
- (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的方法
- 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]]];
或者
Java代碼
NSString *str = [NSString stringWithFormat:@""];
[webview loadHTMLString:str baseURL:[NSURL URLWithString:@"http://www.ibtimes.com"]];
C代碼
NSString *webpage = [NSBundle pathForResource:@"webpage" ofType:@"html" inDirectory:[[NSBundle mainBundle] bundlePath]];
[uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:webpage]]];
C代碼
[uiwebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://qq.com"]]];
**NSBundle的用法 **
NSBundle的對象可以獲取應(yīng)用程序安裝目錄的附件。附件包括了白魂,當前應(yīng)用程序下汽纤,所有的文件。(圖片福荸、屬性列表等)
獲取XML文件
C代碼
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"];
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
獲取TXT文件
C代碼
NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"myFile" ofType:@"txt"];
NSData *data = [NSData dataWithContentsOfFile:filePath];
獲取屬性列表
C代碼
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]];
默認safari打開鏈接地址
.h
C代碼
#import
@interface adFullScreen : UIViewController
{
IBOutlet UIWebView *webview;
}
@end
.m
Java代碼
- (void) viewDidLoad
{
NSString *adHTML = @"";
[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;
}
如果不想設(shè)置點擊,而是打開后直接跳轉(zhuǎn)一個網(wǎng)址蕴坪,只要用以下代碼來代替
Java代碼
- (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;
}
他的其他方法和屬性是:
C代碼
typedef enum {
UIWebViewNavigationTypeLinkClicked,
UIWebViewNavigationTypeFormSubmitted,
UIWebViewNavigationTypeBackForward,
UIWebViewNavigationTypeReload,
UIWebViewNavigationTypeFormResubmitted,
UIWebViewNavigationTypeOther
} UIWebViewNavigationType;
C代碼
@protocol UIWebViewDelegate
@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