如何通过viewView保存访问过的页面?和如何获取并解析服务器端发送过来的json数据?通过一个简单的Demo来学习一下吧!

操作步骤:

1.创建SingleViewApplication应用,新建VIewController,并在xib试图中添加WebView,继承webview的Delegate协议。

2.将ViewController类遵循UIWebViewDelegate和NSURLConnectionDataDelegate协议,并且实现协议中的方法。

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController<UIWebViewDelegate,NSURLConnectionDataDelegate>
@property (retain, nonatomic) IBOutlet UIWebView *webview;
@property (retain, nonatomic) UIAlertView * alert;
@property (retain, nonatomic) IBOutlet UITextField *searchText;
- (IBAction)searchClick:(id)sender; @property (retain, nonatomic) IBOutlet UILabel *label;
@property (retain, nonatomic) NSURLConnection * connection;
@property (retain, nonatomic) NSMutableData * data;
@end

VIewController.m:

#import "ViewController.h"

@interfaceViewController ()

@end

@implementation ViewController

- (void)viewDidLoad

{

[superviewDidLoad];

// Do any additional setup after loading the view, typically from a nib.

//请求网络页面

//    NSURL * url = [NSURL URLWithString:@"http://www.taobao.com"];   //一定要加http://

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

//

self.label.text = @"正在请求数据";

//step1:请求地址

//保存页面

//NSString * urlString = @"http://www.baidu.com";

//访问服务器获取json数据

NSString * urlString = @"http://www.weather.com.cn/data/cityinfo/101020100.html";

NSURL * url = [NSURL URLWithString:urlString];

//step2:实例化一个request

NSURLRequest * request = [NSURLRequestrequestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicytimeoutInterval:30.0];

//step3:创建链接

self.connection = [[NSURLConnectionalloc] initWithRequest:request delegate:self];

if(self.connection)

{

NSLog(@"创建链接成功");

}else{

NSLog(@"创建链接失败");

}

[url release];

[urlString release];

}

- (void)didReceiveMemoryWarning

{

[superdidReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

- (void)dealloc {

[_webview release];

[_searchTextrelease];

[_label release];

[super dealloc];

}

//获取数据

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

//接受一个服务端回话,再次一般初始化接受数据的对象

//NSLog(@"返回数据类型%@",[response ]);

//NSLog(@"返回数据编码%@",[response text]);

NSMutableData * data = [[NSMutableDataalloc] init];

self.data = data;

[data release];

}

//不断的获取数据

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

//接受返回数据,这个方法可能会被调用多次,因此将多次返回数据加起来

NSInteger datalength = [data length];

NSLog(@"返回数据量:%d",datalength);

[self.data appendData:data];

}

//获取文件地址

-(NSString *)dataFilePath:(NSString*)fileName

{

NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *document=[paths objectAtIndex:0];

return [document stringByAppendingPathComponent:fileName];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

//    //连接结束

NSLog(@"%d",[self.data length]);

self.label.text = @"请求结束";

//可以下载图片

//[self.data writeToFile:[self dataFilePath:@"image.jpg"] atomically:YES];

NSString * mystr = [[NSStringalloc] initWithData:self.dataencoding:NSUTF8StringEncoding];

[mystr writeToFile:[self dataFilePath:@"百度图片—全球最大中文图片库.html"] atomically:YES encoding:NSUTF8StringEncoding error:nil];

NSLog(@"最后的结果%@",mystr);

[mystr release];

//    NSDictionary *weather = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers  error:nil];

//    NSLog(@"%@",weather);

//    [weather writeToFile:[self dataFilePath:@"weather.plist"] atomically:YES];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

self.label.text = @"连接失败";

}

- (IBAction)searchClick:(id)sender {

}

@end



结果:

2013-08-27 16:09:18.821 WebViewDemo[673:c07] 创建链接成功

2013-08-27 16:09:19.161 WebViewDemo[673:c07] 返回数据量:153

2013-08-27 16:09:19.161 WebViewDemo[673:c07] 153

2013-08-27 16:09:19.193 WebViewDemo[673:c07] 最后的结果{"weatherinfo":{"city":"上海","cityid":"101020100","temp1":"32℃","temp2":"27℃","weather":"多云","img1":"d1.gif","img2":"n1.gif","ptime":"11:00"}}

[IOS]UIWebView实现保存页面和读取服务器端json数据的更多相关文章

  1. 保存json数据到本地和读取本地json数据

    private void saveJson(JsonBean bean) { File file = new File(getFilesDir(), "json.txt"); Bu ...

  2. JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox

    JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox <html> <head> </h ...

  3. Nginx下HTML页面POST请求静态JSON数据返回405状态

    在浏览器访问HTML页面,发现一些静态JSON数据没有显示,F12查看,如下图所示: 可以看到请求方式为POST 将请求链接复制在浏览器地址栏访问,可以正常请求到数据 F12查看,可以看到请求方式为G ...

  4. unity3d在Android端读取修改Json数据

    首先我们需要下载一个文件 LitJson.dll(下载链接 ps: 是用自己的百度云盘下载的如果链接过时,请留言或自行下载, 密码: 5foa) 另外,由于我们要发布到安卓手机上,所以需要配置Jar和 ...

  5. 服务器端json数据文件分割合并解决方案

    问题引入 Json 是什么就不多说了,本文把Json理解成一种协议. 印象之中,Json貌似是前端的专属,其实不然,服务器端组织数据,依然可以用Json协议. 比如说,某公司有一套测评题目(基于Jso ...

  6. js读取解析JSON数据

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

  7. 静态页面中如何传json数据

    首页传递参数组装成json数据,再编码 var param="{type:'"+type+"',text:'"+select_text+"',sele ...

  8. C# 读取网页JSON数据

    场景描述: 公司和别的系统需要对接,现在对方提供一个网址,数据都是json字符串,我需要对json数据进行处理. 提供的json数据如下格式 一.读取网址中的json数据 public string ...

  9. js读取解析JSON数据(转)

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,是理想的数据交换格式.同时,JSON是 JavaScript 原生格式,这意 ...

随机推荐

  1. MSSQL - SQL Server2008附加数据库失败 错误号:5120

    附加数据库时,显示错误,错误信息为 一种解决方法为,设置mdf文件所在文件夹的权限(有些资料说只设置mdf文件的权限就好,但我试了不管用),在文件夹上右击——属性——安全,如图所示: 选择组或用户名中 ...

  2. ASP.NET - 上传图片方法(单张)

    /// <summary> /// 上传图片 /// </summary> /// <param name="fileupload">上传的控件 ...

  3. 微信JSAPI支付(比较详细) 关于getRrandWCPayRequest:fail_invalid appid 错误

    原文:微信JSAPI支付(比较详细) 关于getRrandWCPayRequest:fail_invalid appid 错误 首先微信支付需注册  微信公从平台开发 和 微信支付商户平台 关于4个密 ...

  4. 我是如何同时拿到阿里和腾讯offer的

    前言 三月真是一个忙碌的季节,刚刚开学就需要准备各种面试和笔试(鄙视).幸运的是,在长达一个月的面试内推季之后,终于同时拿到了阿里和腾讯的offer,还是挺开心的.突而想起久未更新的博客,就冒昧学一学 ...

  5. 获取信息的有关Windows API(最有意思是OpenProcess和GetProcessMemoryInfo)

    1.窗口信息MS为我们提供了打开特定桌面和枚举桌面窗口的函数.hDesk = OpenDesktop(lpszDesktop, 0, FALSE, DESKTOP_ENUMERATE);// 打开我们 ...

  6. Javascript selection的兼容性写法介绍

    本文为大家讲解下Javascript selection的兼容性写法,感兴趣的朋友可以参考下 function getSelectedText() { //this function code is ...

  7. Swift - 带结果列表的搜索条(UISearchDisplayController)的用法

    (注:自iOS8起,苹果便废弃UISearchDisplayController的使用,改为使用UISearchController来实现类似功能,可参考我的另一篇文章“Swift - 使用UISea ...

  8. html中加入超链接方式的汇总

    在CSS样式中,对超链接的样式有以下几种定义(1)设置链接未被访问时的样式,具体写法如下:a:link{font-size:10px;... }(2)设置链接在鼠标经过时的样式,具体写法如下:a:ho ...

  9. 在bmp上添加字符

    //打开位图文件,得到位图句柄   HBITMAP OpenBmpFile(HDC hDC, LPSTR lpszFileName)   {       HBITMAP hBmp = NULL;    ...

  10. 奋斗的孩子的TableView(三篇文章)

    http://blog.sina.com.cn/s/blog_a6fb6cc90101i8it.html http://blog.sina.com.cn/s/blog_a6fb6cc90101hhse ...