欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

在iOS应用中使用UIWebView创建简单的网页浏览器界面

程序员文章站 2023-11-18 13:55:58
uiwebview是ios sdk中一个最常用的控件。是内置的浏览器控件,我们可以用它来浏览网页、打开文档等等。这篇文章我将使用这个控件,做一个简易的浏览器。如下图:...

uiwebview是ios sdk中一个最常用的控件。是内置的浏览器控件,我们可以用它来浏览网页、打开文档等等。这篇文章我将使用这个控件,做一个简易的浏览器。如下图:

在iOS应用中使用UIWebView创建简单的网页浏览器界面

我们创建一个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,布局如下:

在iOS应用中使用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];
}