如何通过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 = [NSURLURLWithString:urlString];

//step2:实例化一个request

NSURLRequest * request = [NSURLRequestrequestWithURL:urlcachePolicy: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 = [datalength];

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

[self.dataappendData:data];

}

//获取文件地址

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

{

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

NSString *document=[pathsobjectAtIndex:0];

return [documentstringByAppendingPathComponent:fileName];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

//    //连接结束

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

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

//可以下载图片

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

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

[mystr writeToFile:[selfdataFilePath:@"百度图片—全球最大中文图片库.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"}}

==================== 迂者 丁小未 CSDN博客专栏=================

MyBlog:http://blog.csdn.net/dingxiaowei2013             MyQQ:1213250243

Unity QQ群:858550         cocos2dx QQ群:280818155

====================== 相互学习,共同进步 ===================

转载请注明出处:http://blog.csdn.net/dingxiaowei2013/article/details/10399799

欢迎关注我的微博:http://weibo.com/u/2590571922

转载于:https://blog.51cto.com/dingxiaowei/1366416

[IOS]UIWebView实现保存页面和读取服务器端json数据相关推荐

  1. ios 获取html中的json数据,[IOS]UIWebView实现保存页面和读取服务器端json数据

    #import"ViewController.h" @interfaceViewController() @end @implementation ViewController - ...

  2. Android studio获取服务器端json数据——天气预报(仅读取了json文件尚未分析)

    Android studio获取服务器端json数据--天气预报 MainActivity.java import androidx.appcompat.app.AppCompatActivity;i ...

  3. freetds php mssql 中文乱码,PHP读取mssql json数据中文乱码的解决办法

    PHP及网页使用UTF-8编码,数据库是sql server2008,使用默认编码(936,即GBK编码) 当读取数据库数据时,使用php自带的json_encode()返回到前端,结果中文不显示. ...

  4. socket 读取 所有 数据 java_Java Socket 读取服务器端返回数据

    //客户端 //1.创建客户端Socket,指定服务器地址和端口 Socket socket =newSocket("localhost",10086); //2.获取输出流,向服 ...

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

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

  6. android端获取Tomcat服务器端json数据并通过listview显示

    大体描述: 大体意思是用eclipse ee创建一个Javaweb项目,该项目能从MySQL数据库中获取user表的数据,将数据封装成json格式,将此项目发布到本地Tomcat服务器,在androi ...

  7. js读取解析JSON数据

    简介 1.JSON 指的是 JavaScript 对象表示法(JavaScript Object Notation) JSON 2.是轻量级的文本数据交换格式. JSON是 JavaScript 原生 ...

  8. 【日记】c# 读取网页json数据

    例:读取双色球官网历史开奖号码数据 准备工作 web url: http://www.cwl.gov.cn/kjxx/ssq/kjgg/ 按F12在网络面板得到 json url: http://ww ...

  9. Android实战简易教程-第六十六枪(服务器端搭建和服务器端Json数据交互)

    学习Android有一段时间了,对服务器端有很深的好奇,决定对服务器端的实现进行一些研究,这里实现了一个简单的小例子,用于获取服务器端的json数据,例子很简单,适合初学者学习使用. 服务器端 首先我 ...

最新文章

  1. LeetCode中等题之简易银行系统
  2. react ui框架_顶级React组件库推荐
  3. superset安装配置
  4. C语言realtime stats实时统计(附完整源码)
  5. spring mvc 提示_Spring BootHibernate提示
  6. python画正方体_python绘制立方体的方法
  7. GitLab 简易指引(三):备份与恢复
  8. exit()函数_complete函数
  9. 【转】sqlserver使用sql导出索引
  10. 外表加载oracle,opengauss外部表插件——oracle_fdw
  11. 关于struct和class的知识
  12. 设置HTML元素的透明度
  13. 【黑盒测试用例设计方法5】正交试验法及其示例
  14. matlab差分法求解ppt,有限差分法求解薛定谔方程_宫建平.pdf
  15. 郎咸平:用友和金蝶的长板在哪儿
  16. Redis数据库的部署及常用命令
  17. 【转载】R语言dplyr包学习笔记(吐血整理宇宙无敌详细版)
  18. opencv-OpenCV中的图像处理 [1]
  19. 【ESP32教程】ESP32EEPROM的使用(使用示例中的eeprom class用法)
  20. 宇宙第一帅的HTML笔记

热门文章

  1. phpsso.php 注入漏洞,PHPCMS各种注入漏洞补丁
  2. mimo的误码率_揭晓MU-MIMO黑科技!
  3. linux查找项目中的问题,教你如何快速定位项目中慢查询[项目管理]
  4. php 变更 obj,PHP: 不向后兼容的变更 - Manual
  5. iir数字滤波器_手把手教系列之一阶数字滤波器设计实现(附代码)
  6. PHP TP5入门 二:写接口,添加控制器并访问
  7. 小程序给视频加默认图片封面,点击播放视频并停止播放上一个视频
  8. 制作 Swift 和 Objective-C Mixed 的 Pod
  9. python批量爬取文档
  10. CentOS源码安装GitLab汉化版第3版