AppDelegate.m
#import "AppDelegate.h"
#import "MainViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
-(void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
MainViewController *mainVC=[[MainViewController alloc]init];
UINavigationController *naVC=[[UINavigationController alloc]initWithRootViewController:mainVC];
self.window.rootViewController=naVC;
[mainVC release];
[naVC release];
return YES;
}
// 协议传值
NetWorkingDelegate.h
#import <Foundation/Foundation.h>
#pragma mark 第一步 声明一个协议
@protocol NetWorkingDelegate <NSObject>
-(void)getData:(id)result;
@end
@interface NetWorkingDelegate : NSObject
#pragma mark 第二步 设置代理人属性
@property(nonatomic,assign)id<NetWorkingDelegate>delegate;
-(void)netWorkingWithURL:(NSString *)strURL;
+(void)netWorkingWithURL:(NSString *)strURL delegate:(id<NetWorkingDelegate>)delegate;
@end
NetWorkingDelegate.m
#import "NetWorkingDelegate.h"
@implementation NetWorkingDelegate
-(void)netWorkingWithURL:(NSString *)strURL
{
/*
因为没有办法保证网址中没有中文,所以需要先对传过来的字符串进行编码,转换.
*/
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 对数据进行JSON解析
id result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// 对已经处理好的数据通过协议方法带到VC里
[self.delegate getData:result];
}];
}
+(void)netWorkingWithURL:(NSString *)strURL delegate:(id<NetWorkingDelegate>)delegate
{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
// 用JSON解析数据
id result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// 类方法,传过来的参数是delegate 直接用delegate调用方法
[delegate getData:result];
}];
}
Block传值
NetWorkingTool.h
#import <Foundation/Foundation.h>
#pragma mark 这个类通过block的方法,把这个类请求的数据,返回到视图控制器
typedef void (^Block)(id result);
@interface NetWorkingTool : NSObject
-(void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
+(void)netWorkingWithURL:(NSString *)strURL block:(Block)block;
// 实现POST请求
+(void)netWorkingWithURL:(NSString *)strURL bodyStr:(NSString *)bodyStr POSTblock:(Block)block;
@end
NetWorkingTool.m
#import "NetWorkingTool.h"
@implementation NetWorkingTool
-(void)netWorkingWithURL:(NSString *)strURL block:(Block)block
{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
// 把JSON处理好的数据,通过block进行回调,返回到视图控制器
block(result);
}];
}
+(void)netWorkingWithURL:(NSString *)strURL block:(Block)block
{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
block(result);
}];
}
+(void)netWorkingWithURL:(NSString *)strURL bodyStr:(NSString *)bodyStr POSTblock:(Block)block
{
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url=[NSURL URLWithString:strEncode];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSData *bodyData=[bodyStr dataUsingEncoding:NSUTF8StringEncoding];
[request setHTTPBody:bodyData];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
id result=[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
block(result);
}];
}
@end
MainViewController.m
#import "MainViewController.h"
#import "NetWorkingDelegate.h"
#import "NetWorkingTool.h"
@interface MainViewController ()<NetWorkingDelegate>
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 上面是视图的创建,在它的下面请求数据
// 请求数据
[self netData];
[self toolData];
}
-(void)netData
{
// 创建网络工程类的对象
NetWorkingDelegate *tool=[[NetWorkingDelegate alloc]init];
// 设置代理人
tool.delegate=self;
// 让对象去调用方法
[tool netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295"];
// 使用加号方法进行网络请求
[NetWorkingDelegate netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295" delegate:self];
}
-(void)toolData
{
// 减号方法,先创建一个工具类的对象
NetWorkingTool *tool=[[NetWorkingTool alloc]init];
// 调用方法
[tool netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295" block:^(id result) {
// result 就是解析完的数据
NSMutableDictionary *dic=result;
NSLog(@"%@",dic);
}];
// 用加号方法进行网络请求
[NetWorkingTool netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295" block:^(id result) {
// 具体实现部分
NSMutableDictionary *dic=result;
NSLog(@"%@",dic);
}];
// POST用加号方法进行网络请求
[NetWorkingTool netWorkingWithURL:@"http://ipad-bjwb.bjd.com.cn/DigitalPublication/publish/Handler/APINewsList.ashx" bodyStr:@"date=20131129&startRecord=1&len=30&udid=1234567890&terminalType=Iphone&cid=213" POSTblock:^(id result) {
NSMutableDictionary *dic=result;
NSLog(@"%@",dic);
}];
}
-(void)getData:(id)result
{
NSMutableDictionary *dic=result;
NSLog(@"%@",dic);
}