1
有什麼辦法,以保持在一個類中我們的Web服務委託方法,並在需要時使用它們?通常我以前把所有的類如didReceiveResponse
和didReceiveData
等書面委託..;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.
}];}