uiwebview是ios sdk中一個最常用的控件。是內置的瀏覽器控件,我們可以用它來瀏覽網頁、打開文檔等等。這篇文章我將使用這個控件,做一個簡易的瀏覽器。如下圖:
我們創建一個window-based application程序命名為:uiwebviewdemo
uiwebview的loadrequest可以用來加載一個url地址,它需要一個nsurlrequest參數。我們定義一個方法用來加載url。在uiwebviewdemoviewcontroller中定義下面方法:
- (void)loadwebpagewithstring:(nsstring*)urlstring
{
nsurl *url =[nsurl urlwithstring:urlstring];
nslog(urlstring);
nsurlrequest *request =[nsurlrequest requestwithurl:url];
[webview loadrequest:request];
}
在界面上放置3個控件,一個textfield、一個button、一個uiwebview,布局如下:
在代碼中定義相關的控件:webview用于展示網頁、textfield用于地址欄、activityindicatorview用于加載的動畫、buttonpress用于按鈕的點擊事件。
@interface uiwebviewdemoviewcontroller :uiviewcontroller<uiwebviewdelegate> {
iboutlet uiwebview *webview;
iboutlet uitextfield *textfield;
uiactivityindicatorview *activityindicatorview;
}
- (ibaction)buttonpress:(id) sender;
- (void)loadwebpagewithstring:(nsstring*)urlstring;
@end
使用ib關聯他們。
設置uiwebview,初始化uiactivityindicatorview:
- (void)viewdidload
{
[super viewdidload];
webview.scalespagetofit =yes;
webview.delegate =self;
activityindicatorview = [[uiactivityindicatorview alloc]
initwithframe : cgrectmake(0.0f, 0.0f, 32.0f, 32.0f)] ;
[activityindicatorview setcenter: self.view.center] ;
[activityindicatorview setactivityindicatorviewstyle: uiactivityindicatorviewstylewhite] ;
[self.view addsubview : activityindicatorview] ;
[self buttonpress:nil];
// do any additional setup after loading the view from its nib.
}
uiwebview主要有下面幾個委托方法:
1、- (void)webviewdidstartload:(uiwebview *)webview;開始加載的時候執行該方法。
2、- (void)webviewdidfinishload:(uiwebview *)webview;加載完成的時候執行該方法。
3、- (void)webview:(uiwebview *)webview didfailloadwitherror:(nserror *)error;加載出錯的時候執行該方法。
我們可以將activityindicatorview放置到前面兩個委托方法中。
- (void)webviewdidstartload:(uiwebview *)webview
{
[activityindicatorview startanimating] ;
}
- (void)webviewdidfinishload:(uiwebview *)webview
{
[activityindicatorview stopanimating];
}
buttonpress方法很簡單,調用我們開始定義好的loadwebpagewithstring方法就行了:
- (ibaction)buttonpress:(id) sender
{
[textfield resignfirstresponder];
[self loadwebpagewithstring:textfield.text];
}
當請求頁面出現錯誤的時候,我們給予提示:
- (void)webview:(uiwebview *)webview didfailloadwitherror:(nserror *)error
{
uialertview *alterview = [[uialertview alloc] initwithtitle:@"" message:[error localizeddescription] delegate:nil cancelbuttontitle:nil otherbuttontitles:@"ok", nil];
[alterview show];
[alterview release];
}
動態獲取uiwebview高度
監聽 webview的 contentsize,每當contentsize的值改變時就去更改webview 的frame。
[activitywebview.scrollview addobserver:self forkeypath:@"contentsize" options:nskeyvalueobservingoptionnew context:nil];
然后在回調方法里改變webview的frame
- (void)observevalueforkeypath:(nsstring *)keypath ofobject:(id)object change:(nsdictionary *)change context:(void *)context
{
if ([keypath isequaltostring:@"contentsize"]) {
webviewheight = [[activitywebview stringbyevaluatingjavascriptfromstring:@"document.body.offsetheight"] floatvalue];
cgrect newframe = activitywebview.frame;
newframe.size.height = webviewheight;
activitywebview.frame = newframe;
[maintableview settableheaderview:activitywebview];
}
}
在頁面消失時記得 remove 監聽對象,否則會閃退
-(void)viewwilldisappear:(bool)antimated{
[super viewwilldisappear:antimated];
[activitywebview.scrollview removeobserver:self
forkeypath:@"contentsize" context:nil];
}