gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章在iOS应用中使用UIWebView创建简单的网页浏览器界面由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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];
}

最后此篇关于在iOS应用中使用UIWebView创建简单的网页浏览器界面的文章就讲到这里了,如果你想了解更多关于在iOS应用中使用UIWebView创建简单的网页浏览器界面的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com