iOS的Objective C的Webservice的
问题描述:
有什么办法,以保持在一个类中我们的Web服务委托方法,并在需要时使用它们?通常我以前把所有的类如didReceiveResponse
和didReceiveData
等书面委托..;iOS的Objective C的Webservice的
答
有一种方法做你想要达到的目标,但一定要适可而止。太多的单身使用的通常被认为是不好的做法。
做一个辛格尔顿,你可以在你的应用程序中使用。
在你单身:APISingleton
-(void)APIRequestWithParams:(NSMutableDictionary*)params
onCompletion:(JSONResponseBlock)completionBlock
onFailure:(OperationFailureBlock)failureBlock{
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
[manager.requestSerializer setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[manager GET:kGymEverAPIfullUrl
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject) {
completionBlock(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
failureBlock(operation);
}];}
如何使用:
-(void)getImages{
NSMutableDictionary *params = [[NSMutableDictionary alloc]initWithObjectsAndKeys:[NSString stringWithFormat:@"%ld",1],@"page",@"5",@"count", nil];
[[APISingleton sharedInstance] APIRequestWithParams:params onCompletion:^(id json) {
postsArray = [json objectForKey:@"posts"];
[self downloadImages];
} onFailure:^(AFHTTPRequestOperation *operation) {
//Do whatever you want if it does not work.
}];}