[IOS]UIWebView实现保存页面和读取服务器端json数据
如何通过viewView保存访问过的页面?和如何获取并解析服务器端发送过来的json数据?通过一个简单的Demo来学习一下吧!操作步骤:1.创建SingleViewApplication应用,新建VIewController,并在xib试图中添加WebView,继承webview的Delegate协议。2.将ViewController类遵循UIWebViewDelegate和NSURLConnectionDataDelegate协议,并且实现协议中的方法。ViewController.h:
@end- ({
// NSURL * url = [NSURL URLWithString:@"// NSURLRequest * request = [NSURLRequest requestWithURL:url];
// [self.webview loadRequest:request];
//
//
// //html// NSString * str = [[NSBundle mainBundle] pathForResource:@"// str = [NSString stringWithContentsOfFile:str encoding:NSUTF8StringEncoding error:nil];
// NSLog(@"%@",str);
// [self.webview loadHTMLString:str baseURL:[[NSBundle mainBundle]bundleURL]];
//
{
} }
[url [urlString
}
- ({
}
- ( [ [ [}
//获取数据
-({
[data }
//不断的获取数据
-({
[}
//获取文件地址
-({
}
-({
// //
[mystr [mystr // NSDictionary *weather = [NSJSONSerialization JSONObjectWithData:self.data options:NSJSONReadingMutableContainers error:nil];
// NSLog(@"%@",weather);
// [weather writeToFile:[self dataFilePath:@"weather.plist"] atomically:YES];
}
-({
}
- (}
@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"}}