我想知道如何从不同的类调用AFHTTPSessionManagern。目前我使用此代码来获取响应,但我不知道如何将其转换为需要参数和回发链接的单例类。我只接收和处理JSON代码。
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];
}];
答案 0 :(得分:2)
您可以使用委派方法。
创建一个单独的类(Sync.h和Sync.m),它只包含上面创建AFHTTPSessionManager,参数和启动连接的代码。在此类中创建委托方法。 现在从你的其他类(mainController),创建一个上面的类和sync.delegate = self的对象。 一旦收到repsonse /错误。委托在mainController中实现的已定义方法。
以下是示例: 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
@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
-(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
}
答案 1 :(得分:0)
您可以使用NSNotificationCenter并通过它传递响应。这有额外的好处,即多个班级可以同时收听。如果数据影响UI,则会为他们提供相应更新的机会。
[manager GET:@"/list-21.aspx?" parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
self.someDictionary = responseObject[@"User"];
[self.tableView reloadData];
[[NSNotificationCenter defaultCenter] postNotificationName:@"USER_DATA_ARRIVED" object:responseObject[@"User"]];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[[NSNotificationCenter defaultCenter] postNotificationName:@"USER_DATA_ARRIVED" object:nil];
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Films"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles:nil];
[alertView show];
}];
添加通知名称的观察者,数据将包含在notification.object
中,如果失败则为零