Use common NSObject
Class for Calling WS with AFNetworking 3.0
This is my Duplicate Answer but it was Updated with AFNetworking 3.0
First make NSObject
Class with any name here i am creating NSObject
Class With name Webservice.h and Webservice.m
Webservice.h
@interface Webservice : NSObject
+ (void)requestPostUrl:(NSString *)strURL parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure;
@end
Webservice.m your nsobject.m file is look like this.(add two functions in .m file)
#import "Webservice.h"
#define kDefaultErrorCode 12345
@implementation Webservice
+ (void)requestPostUrl:(NSString *)strURL parameters:(NSDictionary *)dictParams success:(void (^)(NSDictionary *responce))success failure:(void (^)(NSError *error))failure {
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
[manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
[manager POST:strURL parameters:dictParams progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
if([responseObject isKindOfClass:[NSDictionary class]]) {
if(success) {
success(responseObject);
}
}
else {
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingAllowFragments error:nil];
if(success) {
success(response);
}
}
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
if(failure) {
failure(error);
}
}];
}
@end
make sure you have to replace your dictionary key with success and
message for handling of responce callback function
Use like this call this common method from any viewcontroller.m and any methods from any viewControllers
. for temporary i am using viewDidLoad
for calling This WS.
- (void)viewDidLoad {
[super viewDidLoad];
NSDictionary *dictParam = @{@"parameter1":@"value1",@"parameter1":@"value2"};
[Webservice requestPostUrl:@"add your webservice URL here" parameters:dictParam success:^(NSDictionary *responce) {
//Success
NSLog(@"responce:%@",responce);
//do code here
} failure:^(NSError *error) {
//error
}];
}
add your Parameter, values and webservice URL in above method. you can easily use this NSObjcet
Class. for more details please visit AFNetworking 3.0 or my old answear with AFNetworking 2.0.