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

iOS开发中WebView的基本使用方法简介

程序员文章站 2022-05-16 11:34:45
1、使用uiwebview加载网页 运行xcode 4.3,新建一个single view application,命名为webviewdemo。 2、加载we...

1、使用uiwebview加载网页
运行xcode 4.3,新建一个single view application,命名为webviewdemo。

iOS开发中WebView的基本使用方法简介

2、加载webview
在viewcontroller.h添加webview成员变量和在viewcontroller.m添加实现

复制代码 代码如下:

#import <uikit/uikit.h>

@interface viewcontroller : uiviewcontroller
{
    uiwebview *webview;
}
@end
viewcontroller.m
- (void)viewdidload
{
    [super viewdidload];
    webview = [[uiwebview alloc] initwithframe:cgrectmake(0, 0, 320, 480)];
    nsurlrequest *request =[nsurlrequest requestwithurl:[nsurl urlwithstring:@"http://www.baidu.com"]];
    [self.view addsubview: webview];
    [webview loadrequest:request];
}


运行,这样百度网页就打开了

iOS开发中WebView的基本使用方法简介

手机的网络环境是实时变化的,网络慢的时候,怎么提示用户网页正在打开呢?在网页打开出错的时候怎么提示用户呢?这时候我们就需要知道网页什么时候打开的,
什么时候加载完成,什么时候出错了。那么我们需要实现这个<uiwebviewdelegate>协议
3、实现协议,在viewcontroller.h修改如下:

复制代码 代码如下:

#import <uikit/uikit.h> 
 
@interface viewcontroller : uiviewcontroller<uiwebviewdelegate> 

    uiwebview *webview; 

@end 

按住control+command+向上键,切换到viewcontroller.m文件,这是我们在文件中打入- (void) webview,就能看到如下实现方法:

iOS开发中WebView的基本使用方法简介

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

5、加载等待界面
为了给用户更直观的界面效果,我们加上等待的loading界面试试
在webviewdidstartload加入等待
复制代码 代码如下:

<strong>- (void) webviewdidstartload:(uiwebview *)webview
{
    //创建uiactivityindicatorview背底半透明view    
    uiview *view = [[uiview alloc] initwithframe:cgrectmake(0, 0, 320, 480)]; 
    [view settag:108]; 
    [view setbackgroundcolor:[uicolor blackcolor]]; 
    [view setalpha:0.5]; 
    [self.view addsubview:view]; 
   
    activityindicator = [[uiactivityindicatorview alloc] initwithframe:cgrectmake(0.0f, 0.0f, 32.0f, 32.0f)]; 
    [activityindicator setcenter:view.center]; 
    [activityindicator setactivityindicatorviewstyle:uiactivityindicatorviewstylewhite]; 
    [view addsubview:activityindicator]; 

    [activityindicator startanimating];
   </strong>


加载完成或失败时,去掉loading效果
复制代码 代码如下:

<strong>- (void) webviewdidfinishload:(uiwebview *)webview
{
    [activityindicator stopanimating];
    uiview *view = (uiview*)[self.view viewwithtag:108];
    [view removefromsuperview];
    nslog(@"webviewdidfinishload");

}
- (void) webview:(uiwebview *)webview didfailloadwitherror:(nserror *)error
{
    [activityindicator stopanimating];
    uiview *view = (uiview*)[self.view viewwithtag:108];
    [view removefromsuperview];
    </strong>


运行效果:

iOS开发中WebView的基本使用方法简介