使用UIWebView加载网页

1、使用UIWebView加载网页

运行XCode 4.3,新建一个Single View Application,命名为WebViewDemo。


2、加载WebView

在ViewController.h添加WebView成员变量和在ViewController.m添加实现

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h>   
  2.   
  3. @interface ViewController : UIViewController  
  4. {  
  5.     UIWebView *webView;  
  6. }  
  7. @end  
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
{
UIWebView *webView;
}
@end
[cpp] view plaincopyprint?
  1. ViewController.m  
ViewController.m
[cpp] view plaincopyprint?
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.     webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];  
  5.     NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com"]];  
  6.     [self.view addSubview: webView];  
  7.     [webView loadRequest:request];  
  8. }  
- (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];
}
运行,这样百度网页就打开了


手机的网络环境是实时变化的,网络慢的时候,怎么提示用户网页正在打开呢?在网页打开出错的时候怎么提示用户呢?这时候我们就需要知道网页什么时候打开的,

什么时候加载完成,什么时候出错了。那么我们需要实现这个<UIWebViewDelegate>协议

3、实现协议,在ViewController.h修改如下:

[cpp] view plaincopyprint?
  1. #import <UIKit/UIKit.h>   
  2.   
  3. @interface ViewController : UIViewController<UIWebViewDelegate>  
  4. {  
  5.     UIWebView *webView;  
  6. }  
  7. @end  
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController<UIWebViewDelegate>
{
UIWebView *webView;
}
@end

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



UIWebView中几个重要的函数
1.- (void )webViewDidStartLoad:(UIWebView  *)webView   网页开始加载的时候调用
2.- (void )webViewDidFinishLoad:(UIWebView  *)webView  网页加载完成的时候调用
3.- (void)webView:(UIWebView *)webView  didFailLoadWithError:(NSError *)error 网页加载错误的时候调用

4、实现这三个方法,加入NSLog。

先在viewDidLoad 的webView实例化下面加上

    [webView setDelegate:self];设置代理。这样上面的三个方法才能得到回调。

三个方法实现如下:

[cpp] view plaincopyprint?
  1. <SPAN style="FONT-FAMILY: Arial, Verdana, sans-serif; COLOR: #333333">- (void) webViewDidStartLoad:(UIWebView *)webView  
  2. {  
  3.     NSLog(@"webViewDidStartLoad");  
  4. }  
  5. - (void) webViewDidFinishLoad:(UIWebView *)webView  
  6. {  
  7.     NSLog(@"webViewDidFinishLoad");  
  8. }  
  9. - (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error  
  10. {  
  11.     NSLog(@"didFailLoadWithError:%@", error);  
  12. }  
  13. </SPAN>  
- (void) webViewDidStartLoad:(UIWebView *)webView
{
NSLog(@"webViewDidStartLoad");
}
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
NSLog(@"webViewDidFinishLoad");
}
- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(@"didFailLoadWithError:%@", error);
}
运行打印:

2012-06-23 15:20:29.728 WebViewDemo[1001:f803] webViewDidStartLoad

2012-06-23 15:20:29.991 WebViewDemo[1001:f803] webViewDidFinishLoad

那我们试试error情况,把wifi关掉,运行打印结果:

2012-06-23 15:23:58.939 WebViewDemo[1087:f803] webViewDidStartLoad

2012-06-23 15:23:59.016 WebViewDemo[1087:f803] webViewDidFinishLoad

请求结果不变,为什么关掉网络还成功了呢?缓存?我换163.com试试,这是真正的结果出来了:

2012-06-23 15:24:41.131 WebViewDemo[1134:f803] webViewDidStartLoad

2012-06-23 15:24:41.149 WebViewDemo[1134:f803] didFailLoadWithError:Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline." UserInfo=0x6b41660 {NSErrorFailingURLStringKey=http://www.163.com/, NSErrorFailingURLKey=http://www.163.com/, NSLocalizedDescription=The Internet connection appears to be offline., NSUnderlyingError=0x6eae690 "The Internet connection appears to be offline."}

连接错误了,调用了 didFailLoadWithError。

5、加载等待界面

为了给用户更直观的界面效果,我们加上等待的loading界面试试

在webViewDidStartLoad加入等待

[cpp] view plaincopyprint?
  1. <STRONG>- (void) webViewDidStartLoad:(UIWebView *)webView  
  2. {  
  3.     //创建UIActivityIndicatorView背底半透明View        
  4.     UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];    
  5.     [view setTag:108];    
  6.     [view setBackgroundColor:[UIColor blackColor]];    
  7.     [view setAlpha:0.5];    
  8.     [self.view addSubview:view];    
  9.       
  10.     activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 32.0f, 32.0f)];    
  11.     [activityIndicator setCenter:view.center];    
  12.     [activityIndicator setActivityIndicatorViewStyle:UIActivityIndicatorViewStyleWhite];    
  13.     [view addSubview:activityIndicator];    
  14.   
  15.     [activityIndicator startAnimating];  
  16.    </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];

加载完成或失败时,去掉loading效果

[cpp] view plaincopyprint?
  1. <STRONG>- (void) webViewDidFinishLoad:(UIWebView *)webView  
  2. {  
  3.     [activityIndicator stopAnimating];  
  4.     UIView *view = (UIView*)[self.view viewWithTag:108];  
  5.     [view removeFromSuperview];  
  6.     NSLog(@"webViewDidFinishLoad");  
  7.   
  8. }  
  9. - (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error  
  10. {  
  11.     [activityIndicator stopAnimating];  
  12.     UIView *view = (UIView*)[self.view viewWithTag:108];  
  13.     [view removeFromSuperview];  
  14.     </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];
运行效果:


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/388287.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Java 开源库精选(持续更新)

仅记录亲自使用和考虑使用的Apache Commons Commons IO - Commons IO 是一个帮助开发IO功能的实用程序库 Commons Configuration - Commons Configuration 提供了一个通用配置界面&#xff0c;使Java应用程序可以从各种来源读取配置数据。查看更多可重用、稳定的 Commons 组件S…

corba的兴衰_数据科学薪酬的兴衰

corba的兴衰意见 (Opinion) 目录 (Table of Contents) Introduction 介绍 Salary and Growth 薪资与增长 Summary 摘要 介绍 (Introduction) In the past five years, data science salary cumulative growth has varied between 12% in the United States, according to Glass…

hibernate的多表查询

1.交叉连接 select * from A ,B 2.内连接 可以省略inner join 隐式内连接&#xff1a; select * from A,B where A.id B.aid; 显式内连接&#xff1a; select * from A inner join B on A.id B.aid; 迫切内连接&#xff1a; 需要加上fetch关键字 内连接查询两者共有的属性…

C# 读取PE

最后分析结果会放在 一个DATASET里 ResourceDirectory这个TABLE 增加了 GUID列 为了好实现数结构 using System; using System.IO; using System.Data; using System.Collections; namespace PETEST { /// <summary> /// PeInfo 的摘要说明。 /// zgkesina.com …

10 个深恶痛绝的 Java 异常。。

异常是 Java 程序中经常遇到的问题&#xff0c;我想每一个 Java 程序员都讨厌异常&#xff0c;一 个异常就是一个 BUG&#xff0c;就要花很多时间来定位异常问题。 什么是异常及异常的分类请看这篇文章&#xff1a;一张图搞清楚 Java 异常机制。今天&#xff0c;栈长来列一下 J…

POJ 2777 - Count Color(线段树区间更新+状态压缩)

题目链接 https://cn.vjudge.net/problem/POJ-2777 【题意】 有一个长度为 LLL 的区间 [1,L][1,L][1,L] &#xff0c;有 TTT 种颜色可以涂&#xff0c;有 QQQ 次操作&#xff0c;操作分两种C A B CC \ A \ B \ CC A B C 把区间 [A,B][A,B][A,B] 涂成第 CCC 种颜色P A BP \ A \ …

如何实施成功的数据清理流程

干净的数据是发现和洞察力的基础。 如果数据很脏&#xff0c;您的团队为分析&#xff0c;培养和可视化数据而付出的巨大努力完全是在浪费时间。 当然&#xff0c;肮脏的数据并不是新的。 它早在计算机变得普及之前就困扰着决策。 现在&#xff0c;计算机技术已普及到日常生活中…

nginx前端代理tomcat取真实客户端IP

nginx前端代理tomcat取真实客户端IP2011年12月14日⁄ nginx⁄ 暂无评论⁄ 被围观 3,000 次使用Nginx作为反向代理时&#xff0c;Tomcat的日志记录的客户端IP就不在是真实的客户端IP&#xff0c;而是Nginx代理的IP。要解决这个问题可以在Nginx配置一个新的Header&#xff0c;用来…

kubeadm安装kubernetes 1.13.2多master高可用集群

1. 简介 Kubernetes v1.13版本发布后&#xff0c;kubeadm才正式进入GA&#xff0c;可以生产使用,用kubeadm部署kubernetes集群也是以后的发展趋势。目前Kubernetes的对应镜像仓库&#xff0c;在国内阿里云也有了镜像站点&#xff0c;使用kubeadm部署Kubernetes集群变得简单并且…

通才与专家_那么您准备聘请数据科学家了吗? 通才还是专家?

通才与专家Throughout my 10-year career, I have seen people often spend their time and energy in passionate debates about what data science can deliver, and what data scientists do or do not do. I submit that these are the wrong questions to focus on when y…

ubuntu opengl 安装

安装相应的库&#xff1a; sudo apt-get install build-essential libgl1-mesa-dev sudo apt-get install freeglut3-dev sudo apt-get install libglew-dev libsdl2-dev libsdl2-image-dev libglm-dev libfreetype6-dev 实例&#xff1a; #include "GL/glut.h" void…

分享一病毒源代码,破坏MBR,危险!!仅供学习参考,勿运行(vc++2010已编译通过)

我在编译的时候&#xff0c;杀毒软件提示病毒并将其拦截&#xff0c;所以会导致编译不成功。 1>D:\c工程\windows\windows\MBR病毒.cpp : fatal error C1083: 无法打开编译器中间文件:“C:\Users\lenovo\AppData\Local\Temp\_CL_953b34fein”: Permission denied 1> 1>…

HTTP请求错误400、401、402、403、404、405、406、407、412、414、500、501、502解析

【转载】本文来自 chenxinchongcn 的CSDN 博客 &#xff0c;全文地址请点击&#xff1a;https://blog.csdn.net/chenxinchongcn/article/details/54945998?utm_sourcecopy HTTP 错误 400 400 请求出错 由于语法格式有误&#xff0c;服务器无法理解此请求。不作修改&#xff0…

数据科学家 数据工程师_数据科学家实际上赚了多少钱?

数据科学家 数据工程师目录 (Table of Contents) Introduction 介绍 Junior Data Scientist 初级数据科学家 Mid-Level Data Scientist 中级数据科学家 Senior Data Scientist 资深数据科学家 Additional Compensation 额外补偿 Summary 摘要 介绍 (Introduction) The lucrativ…

Spring Cloud构建微服务架构-Hystrix监控面板

在Spring Cloud中构建一个Hystrix Dashboard非常简单&#xff0c;只需要下面四步&#xff1a;愿意了解源码的朋友直接求求交流分享技术 一零三八七七四六二六 创建一个标准的Spring Boot工程&#xff0c;命名为&#xff1a;hystrix-dashboard。 编辑pom.xml&#xff0c;具体依赖…

Google 地图 API 参考

杨航收集技术资料&#xff0c;分享给大家 Google 地图 API 参考 Google 地图 API 现在与 Google AJAX API 载入器集成&#xff0c;后者创建了一个公共命名空间&#xff0c;以便载入和使用多个 Google AJAX API。该框架可让您将可选 google.maps.* 命名空间用于当前在 Google …

spotify歌曲下载_使用Spotify数据预测哪些“ Novidades da semana”歌曲会成为热门歌曲

spotify歌曲下载TL; DR (TL;DR) Spotify is my favorite digital music service and I’m very passionate about the potential to extract meaningful insights from data. Therefore, I decided to do this article to consolidate my knowledge of some classification mod…

Hook技术之Hook Activity

一、Hook技术概述 Hook技术的核心实际上是动态分析技术&#xff0c;动态分析是指在程序运行时对程序进行调试的技术。众所周知&#xff0c;Android系统的代码和回调是按照一定的顺序执行的&#xff0c;这里举一个简单的例子&#xff0c;如图所示。 对象A调用类对象B&#xff0c…

(第三周)周报

此作业要求https://edu.cnblogs.com/campus/nenu/2018fall/homework/2143 1.本周PSP 总计&#xff1a;1422 min 2.本周进度条 (1)代码累积折线图 (2)博文字数累积折线图 4.PSP饼状图 转载于:https://www.cnblogs.com/gongylx/p/9761852.html

功能测试代码python_如何使您的Python代码更具功能性

功能测试代码pythonFunctional programming has been getting more and more popular in recent years. Not only is it perfectly suited for tasks like data analysis and machine learning. It’s also a powerful way to make code easier to test and maintain.近年来&am…