首页 文章 精选 留言 我的

精选列表

搜索[资源读取],共10000篇文章
优秀的个人博客,低调大师

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

如何通过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 @implementationViewController - (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:requestdelegate:self]; if(self.connection) { NSLog(@"创建链接成功"); }else{ NSLog(@"创建链接失败"); } [urlrelease]; [urlStringrelease]; } - (void)didReceiveMemoryWarning { [superdidReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (void)dealloc { [_webviewrelease]; [_searchTextrelease]; [_labelrelease]; [superdealloc]; } //获取数据 -(void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response { //接受一个服务端回话,再次一般初始化接受数据的对象 //NSLog(@"返回数据类型%@",[response ]); //NSLog(@"返回数据编码%@",[response text]); NSMutableData* data = [[NSMutableDataalloc]init]; self.data= data; [datarelease]; } //不断的获取数据 -(void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data { //接受返回数据,这个方法可能会被调用多次,因此将多次返回数据加起来 NSIntegerdatalength = [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]; [mystrwriteToFile:[selfdataFilePath:@"百度图片—全球最大中文图片库.html"] atomically:YESencoding:NSUTF8StringEncodingerror:nil]; NSLog(@"最后的结果%@",mystr); [mystrrelease]; // 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 本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366416,如需转载请自行联系原作者

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册