2013-01-09 55 views
1

的API I類從AFnetworking到RestKit切換。在AFnetworking有一個API類。 API.h類包含以下內容。寫RestKit框架

#import <UIKit/UIKit.h> 
typedef void (^JSONResponseBlock)(NSDictionary* json); 
@interface API : NSObject 
//the authorized user 
@property (strong, nonatomic) NSDictionary* user; 

+(API*)sharedInstance; 
//check whether there's an authorized user 

//send an API command to the server 
-(void)loginCommand:(NSMutableDictionary*)params onCompletion:(JSONResponseBlock)completionBlock; 

而我的API.m類看起來像這樣。

+(API *)sharedInstance 
{ 
    static API *sharedInstance = nil; 
    static dispatch_once_t oncePredicate; 
    dispatch_once(&oncePredicate,^{ 
     sharedInstance = [[self alloc]initWithBaseURL:[NSURL URLWithString:kAPIHost]]; 
    }); 
    return sharedInstance; 
} 

#pragma mark - init 
//intialize the API class with the destination host name 

-(API *)init 
{ 
    //call super init 
    self = [super init]; 

    if (self != nil){ 
     //initialize the object 
     user = nil; 

     [self registerHTTPOperationClass:[AFJSONRequestOperation class]]; 

     // Accept HTTP Header; see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.1 
     [self setDefaultHeader:@"Accept" value:@"application/json"]; 
    } 
    return self; 
} 

-(void)loginCommand:(NSMutableDictionary *)params onCompletion:(JSONResponseBlock)completionBlock{ 
    NSLog(@"%@%@",kAPIHost,kAPILogin); 
    NSMutableURLRequest *apiRequest = [self multipartFormRequestWithMethod:@"POST" path:kAPILogin parameters:params constructingBodyWithBlock:^(id <AFMultipartFormData>formData){ 
     //TODO: attach file if needed 

    }]; 
    AFJSONRequestOperation *operation = [[AFJSONRequestOperation alloc] initWithRequest:apiRequest]; 
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject){ 
     //success! 
     NSLog(@"SUCCESSSS!"); 
     completionBlock(responseObject); 
    }failure:^(AFHTTPRequestOperation *operation, NSError *error){ 
     //Failure 
     NSLog(@"FAILUREE!"); 
     completionBlock([NSDictionary dictionaryWithObject:[error localizedDescription] forKey:@"error"]); 
    }]; 
    [operation start]; 

} 

就像你可以看到我只實例化一次,並把所有的方法放在這裏。在我的視圖控制器我只需要調用此方法與參數字典。然後我可以讀取整個JSON文件。

現在有了restKit我做這一切的viewController上水平。我想像我通過AFNetworking那樣分割它。這是我做RestKit,目前是這一切的viewController上水平。

//let AFNetworking manage the activity indicator 
[AFNetworkActivityIndicatorManager sharedManager].enabled = YES; 

// Initialize HTTPClient 
NSURL *baseURL = [NSURL URLWithString:@"http://virtuele-receptie.preview.sanmax.be"]; 
AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:baseURL]; 
//we want to work with JSON-Data 
[client setDefaultHeader:@"Accept" value:RKMIMETypeJSON]; 

// Initialize RestKit 
RKObjectManager *objectManager = [[RKObjectManager alloc] initWithHTTPClient:client]; 


//Do mapping 

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:dataMapping 
                        pathPattern:nil 
                         keyPath:@"data" 
                        statusCodes:[NSIndexSet indexSetWithIndex:200]]; 
[objectManager addResponseDescriptor:responseDescriptor]; 

NSDictionary *dict = [[NSDictionary alloc]initWithObjectsAndKeys:_txtLogin.text,@"email",_txtPass.text,@"pwd", nil]; 

[objectManager getObject:nil path:@"/nl/webservice/company-user/login/apikey/key12345678" parameters:dict 
       success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) { 
        //Success 
       } 
       failure:^(RKObjectRequestOperation *operation, NSError *error) { 
        //Failure 
       }]; 

回答

0

到目前爲止,我還沒有看到像使用其他HTTP框架一樣創建API類的巨大需求。 RestKit有它自己的HTTP客戶端(實際上只是AFNetworking的客戶端),所以不需要爲你的HTTP客戶端創建一個類,我發現每次使用RKObjectManager時,我通常都需要訪問方法參數和塊回調在每個視圖控制器內。換句話說,我不想在API類運行RestKit網絡代碼,因爲我將基本上必須包裝在整個呼叫可以在視圖控制器(成功阻滯,不良區等)進行訪問的方法。現在3級或4的應用程序 - -

從本質上說,RestKit的設計減輕了網絡代碼,以至於在我的經驗用它,我還沒有看到足夠的理由寫一個API類像你描述。