您的位置:首页 > 产品设计 > UI/UE

UI15_NetWorkingTool

2015-08-25 08:46 302 查看
//
//  AppDelegate.h
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@end
//
//  AppDelegate.m
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#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];
[_window release];
MainViewController *mainVC=[[MainViewController alloc] init];
UINavigationController *naVC=[[UINavigationController alloc] initWithRootViewController:mainVC];
self.window.rootViewController=naVC;
[mainVC release];
[naVC release];

return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end


//
//  MainViewController.h
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface MainViewController : UIViewController

@end


//
//  MainViewController.m
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#import "MainViewController.h"
#import "NetWorkingDelegate.h"
#import "NetWorkingTOOL.h"
@interface MainViewController ()<NetWorkingDelegate>

@end

@implementation MainViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//上面是视图的创建
//请求数据
//[self netData];
[self toolData];

}
- (void)toolData{
///block get 减号方法
//创建一个工具类的对象
//    NetWorkingTOOL *tool=[[NetWorkingTOOL alloc] init];
//    //调用方法
//    [tool netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295" block:^(id result) {
//        NSMutableDictionary *dic=result;
//        NSLog(@"%@",dic);
//    }];
///block  get  加号方法
//     [NetWorkingTOOL netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295" backBlock:^(id result) {
//    NSMutableDictionary *dic=result;
//          NSLog(@"%@",dic);
//}];
///block 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" backBlock:^(id result) {
NSMutableDictionary *dic=result;
NSLog(@"%@",dic);
}];
}
- (void)netData{
///协议方法 get减号方法
//    //创建对象
//    NetWorkingDelegate *tool=[[NetWorkingDelegate alloc] init];
//    //设置代理人
//    tool.delegate=self;
//    //让对象去调用方法
//    [tool netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295"];
///协议方法 get 加号方法,,类调用..不用创建对象
[NetWorkingDelegate netWorkingWithURL:@"http://api.map.baidu.com/place/v2/search?query=银行®ion=大连&output=json&ak=6E823f587c95f0148c19993539b99295" delegate:self];
}
-(void)getData:(id)result{
NSMutableDictionary *dic=result;
NSLog(@"%@",dic);

}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/

@end


//
//  NetWorkingTOOL.h
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#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
backBlock:(Block)block;
//实现post请求
+(void)netWorkingWithURL:(NSString *)strurl
bodyStr:(NSString *)bodyStr
backBlock:(Block)block;

@end


//
//  NetWorkingTOOL.m
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#import "NetWorkingTOOL.h"

@implementation NetWorkingTOOL
////get block 减号方法
- (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:NSJSONReadingMutableContainers error:nil];
//把json处理好的数据,,用过block进行回调,返回到试图控制器
block(result);
} ];
}
///get block 加号方法
+(void)netWorkingWithURL:(NSString *)strURL
backBlock:(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:NSJSONReadingMutableContainers error:nil];
block(result);
} ];
}
////POST block 加号方法
+(void)netWorkingWithURL:(NSString *)strurl
bodyStr:(NSString *)bodyStr
backBlock:(Block)block{
NSURL *url=[NSURL URLWithString:strurl];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
///---------------------对比get 多一个body体,,多三行代码
[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:NSJSONReadingMutableContainers error:nil];
block(result);
}];
}

@end


//
//  NetWorkingDelegate.h
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#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
//  UI15_NetWorkingTool
//
//  Created by dllo on 15/8/18.
//  Copyright (c) 2015年 flg. All rights reserved.
//

#import "NetWorkingDelegate.h"

@implementation NetWorkingDelegate
- (void)netWorkingWithURL:(NSString *)strURL{
//没办法保证网址没有中文,,所以搜首先把传过来的字符串中中文进行转换
NSString *strEncode=[strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//创建NSURL
NSURL *url=[NSURL URLWithString:strEncode];
//创建请求
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:url];
//block实现异步
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue]completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
//数据json解析
id result=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers 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 getData:result];
}];
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: