Aysnc connecting with server


##1  Using delegate

### .h file
```objectviec

#import <Foundation/Foundation.h>

@protocol GTNetworkUtilsDelegate <NSObject>

@required
-(void) didGetData:(NSMutableDictionary*) recevieMutableDictionary apiName:(NSString*) apiName;
-(void) failedGetData:(NSError*)error  apiName:(NSString*) apiName;
@end

@interface GTNetworkUtils : NSObject<NSURLConnectionDataDelegate>

@property (nonatomic, weak) id <GTNetworkUtilsDelegate> delegate;

-(instancetype) initWithApiUrl:(NSString*) apiUrl type:(NSString*) type;

@end

```

### .m file
```objectviec
#import "GTNetworkUtils.h"

@interface GTNetworkUtils()
@property (nonatomic) NSMutableData *receivedData;
@property (nonatomic) NSString *apiName;
@end

@implementation GTNetworkUtils

-(instancetype) initWithApiUrl:(NSString*) apiUrl type:(NSString*) type
{
    self.apiName = type;
    NSString *addStr = [[GTSystemUtils getPlistInfo:API_DOMAIN] stringByAppendingString:apiUrl];
    NSURL *url = [NSURL URLWithString:[addStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20.0];
    
    NSURLConnection *aConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    
    if (!aConnection) {
        APLog(@"connection error.");
    }
    
    return self;
}

- (void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response {
    self.receivedData = [[NSMutableData alloc] init];
}

- (void) connection:(NSURLConnection *) connection didReceiveData:(NSData *)data {
    [self.receivedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError*)error
{
    APLog(@"error=%@", [error description]);
    [self.delegate failedGetData:error apiName:self.apiName];
}

- (void) connectionDidFinishLoading:(NSURLConnection *)connection {    
    NSMutableDictionary* td = [[NSMutableDictionary alloc] init];
    NSError *error = nil;
    NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:self.receivedData options:NSJSONReadingAllowFragments error:&error];
    
    if(error == nil) {
        
            td = [jsonObject objectForKey:@"data"];
            [self.delegate didGetData:td apiName:self.apiName];
    } else {
        APLog(@"%@",[error description]);
        [self.delegate didGetData:nil apiName:self.apiName];
    }
}
```

## 2 Using block

### .h file
``` objectivec
#import <Foundation/Foundation.h>
#import "GTSystemUtils.h"

typedef void (^CallbackLoginHandler)(NSDictionary * resultDictonary, NSError *error);

@interface GTSevenNetwordUtils : NSObject

-(void) reLoginWithHandler:(CallbackLoginHandler) handler;

@end
```

### .m file
``` objectivec
#import "GTSevenNetwordUtils.h"

@interface GTSevenNetwordUtils()

@end

@implementation GTSevenNetwordUtils

-(void) reLoginWithHandler:(CallbackLoginHandler) handler
{
    NSString *email = [GTSystemUtils getUserDefault:LOGIN_EMAIL];
    NSString *password = [GTSystemUtils getUserDefault:LOGIN_PASSWORD];
    
    NSString *apiStr = [[GTSystemUtils getPlistInfo:SEVEN_SPOT_API_DOMAIN] stringByAppendingString:[[GTSystemUtils getPlistInfo:SEVEN_MEMBER_LOGIN] stringByAppendingString:[NSString stringWithFormat:@"&login_identity=%@&password=%@&mac_address=", email, password]]];
    NSURL *url = [NSURL URLWithString:apiStr];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    
    NSError *error;
    NSURLResponse *response;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    
    NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
    
    if (handler) {
        handler(jsonObject, error);
    }
}


```