AFNetworking 2.0-成功时如何将响应传递给另一个班级 [英] AFNetworking 2.0 - How to pass response to another class on success
问题描述
我想知道如何从不同的类调用AFHTTPSessionManagern。当前,我使用此代码来获取响应,但是我不确定如何将其转换为需要参数和回发链接的单例类。我仅接收和处理JSON代码。
I am wondering how can I call AFHTTPSessionManagern from different class. Currently I use this code to get the response but I am not sure how to convert this to singleton class which requires parameters and postback link. I am receiving and processing JSON code only.
static NSString * const BaseURLString = @"http://www.example.com/";
NSURL *baseURL = [NSURL URLWithString:BaseURLString];
NSDictionary *parameters = @{ @"Token" : @"123456"};
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:@"/list-21.aspx?" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
self.someDictionary = responseObject[@"User"];
[self.tableView reloadData];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Films"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
推荐答案
您可以使用委派方法。
创建一个单独的类(Sync.h和Sync.m),该类仅包含上述创建AFHTTPSessionManager,Parameters和启动连接的代码。在此类中创建委托方法。
现在,从您的其他类(mainController)创建一个以上类的对象,然后sync.delegate = self。
收到回复/错误后。委托给在mainController中实现的已定义方法。
Create a separate class (Sync.h and Sync.m) which only contains above code of creating AFHTTPSessionManager, Parameters and starting the connection. Create delegate method in this class. Now from your other class(mainController), create a object of above class and sync.delegate = self. Once you receive repsonse/error. Delegate to the defined methods which are implemented in mainController.
下面是示例:
SyncManager.h
Below is the example: SyncManager.h
#import <UIKit/UIKit.h>
@protocol SyncDelegate<NSObject>
-(void)syncSuccess:(id) responseObject;
-(void)syncFailure:(NSError*) error;
@end
@interface SyncManager: UIViewController
{
}
-(void) serviceCall:(NSString*)url withParams:(NSDictionary*) params;
@end
SyncManager.m
SyncManager.m
@interface SyncManager ()
@end
@implementation SyncManager
-(void) serviceCall:(NSString*)url withParams:(NSDictionary*) params {
NSURL *baseURL = [NSURL URLWithString:url];
AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
[manager GET:@"/list-21.aspx?" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
[self.delegate syncSuccess:responseObject];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[self.delegate syncFailure:error];
}];
}
@end
ViewController.m
ViewController.m
-(void)fetchdata {
NSDictionary *dictionary = @{@"name":@"Arun"};
SyncManager *sync = [[SyncManager alloc] init];
sync.delegate = self;
[sync serviceCall:@"www.google.com" withparams:dictionary];
}
-(void)syncSuccess:(id) responseObject {
// Parse your data
}
-(void)syncFailure:(NSError*) error {
//display Error
}
这篇关于AFNetworking 2.0-成功时如何将响应传递给另一个班级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!