[IOS]UIWebView 请求网络页面或者加载本地资源页面
2024-10-09 10:11:14
UIWebView是一个能够显示网页的IOS视图控件,我们可以用它来访问一个网站。下面是具体的实例:
操作步骤:
1.首先在xib文件中拖放一个UIWebView控件到view中
2.将下载的页面以及页面资源加载到项目中,但必须选择Create folder references for any added folders,然后知道文件在项目中是蓝色显示,而不是黄色显示
3.将webView的Delegate拖到File's Owner,继承UIWebView的Delegate协议,并且实现他的协议
ViewController.h:
#import <UIKit/UIKit.h> @interface DXWViewController : UIViewController<UIWebViewDelegate>
@property (retain, nonatomic) IBOutlet UIWebView *webview;
@property(nonatomic,retain) UIAlertView *alert;
@end
ViewController.m:
#import "DXWViewController.h" @interface DXWViewController () @end @implementation DXWViewController - (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//[self.webview loadRequest:request]; //加载本地资源,html页面
NSString *str = [[NSBundle mainBundle] pathForResource:@"百度图片—全球最大中文图片库" ofType:@"html"]; str = [NSString stringWithContentsOfFile:str encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str);
[self.webview loadHTMLString:str baseURL:[[NSBundle mainBundle] bundleURL]];
} - (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)dealloc {
[_webview release];
[_alert release];
[super dealloc];
} -(void)webViewDidFinishLoad:(UIWebView *)webView
{
[self.alert dismissWithClickedButtonIndex:0 animated:YES];
} -(void)webViewDidStartLoad:(UIWebView *)webView
{
self.alert = [[UIAlertView alloc] initWithTitle:@"Loading..." message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
[self.alert show]; UIActivityIndicatorView *aiv = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
aiv.center = CGPointMake(self.alert.bounds.size.width/2, self.alert.bounds.size.height/2);
[aiv startAnimating];
[self.alert addSubview:aiv];
} @end
最新文章
- Java web.xml 配置详解
- 深入理解 Javascript 面向对象编程
- 一个NULL引发的血案
- tomcat deploy部署项目三种方法
- PHP慕课网教程
- 51nod 1120 机器人走方格 V3 卡特兰数 lucas定理
- 鸟哥笔记:postfix的一些重要配置文件
- 【原创】MapReduce编程系列之二元排序
- Linux一
- C# 模拟POST提交文件
- C程序设计语言练习题1-13
- 百度静态资源(JS)公共库
- json格式数据,将数据库中查询的结果转换为json, 然后调用接口的方式返回json(方式一)
- 转 ogg组件介绍
- MLDS笔记:Generalization
- Python 遇到的坑
- 7.封装,static,方法重载
- ruby学习-字符串
- python环境和工具
- Spark Structured Stream 2