Full AFNetwork tutorial in swift with code
-------------------------------- APIDemo-Bridging-Header.h -----------------------
#import <AFNetworking/AFNetworking.h>
#import "WebServiceCallClass.h"
---------------------------------- WebServiceCallClass.h --------------------------
#import <Foundation/Foundation.h>
@interface WebServiceCallClass : NSObject
+(void)callWebServiceUsingGetMethodGetURL:(NSString *)strURL successBlock:(void(^)(id responseObject))sucess failureBlock:(void(^)(id errorResponse))fail;
+(void)callWebServiceUsingPostMethodPostURL:(NSString *)strURL withParamiterDicrionary : (NSDictionary *)paramiterDict successPostBlock:(void(^)(id responseObject))sucess failurePostBlock:(void(^)(id errorResponse))fail;
@end
---------------------------------- WebServiceCallClass.m ----------------------------------
#import "WebServiceCallClass.h"
#import <AFNetworking/AFNetworking.h>
@implementation WebServiceCallClass
+(void)callWebServiceUsingGetMethodGetURL:(NSString *)strURL successBlock:(void(^)(id responseObject))sucess failureBlock:(void(^)(id errorResponse))fail
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
[manager GET:strURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
sucess(responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSData *data = [errResponse dataUsingEncoding:NSUTF8StringEncoding];
id dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dict);
fail(dict);
}];
}
+(void)callWebServiceUsingPostMethodPostURL : (NSString *)strURL withParamiterDicrionary : (NSDictionary *)paramiterDict successPostBlock:(void(^)(id responseObject))sucess failurePostBlock:(void(^)(id errorResponse))fail
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
//If send with header security than open this two line.
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
// [manager.requestSerializer setValue:@"123" forHTTPHeaderField:@"token"];
// [manager.requestSerializer setValue:@"1.0.0" forHTTPHeaderField:@"version"];
[manager POST:strURL parameters:paramiterDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSError *e = nil;
NSDictionary *responseDict = [NSJSONSerialization
JSONObjectWithData: responseObject
options: NSJSONReadingMutableContainers
error: &e];
sucess(responseDict);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSLog(@"%@",error);
NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSData *data = [errResponse dataUsingEncoding:NSUTF8StringEncoding];
id dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dict);
fail(dict);
}];
}
@end
-------------------------------------- yourmodel.swift ------------------------------
func getdata()
{
let para = NSMutableDictionary()
// para.setValue("1", forKey: "User_ID")
WebServiceCallClass.callWebService(usingPostMethodPostURL: "YOUR.URL.com", withParamiterDicrionary: para as! [AnyHashable : Any], successPost: { (responseData) in
print(responseData)
if let responsestate = (responseData as AnyObject).value(forKey: "response") as? NSDictionary
{
if let responseData = (responsestate).value(forKey: "responseData") as? NSDictionary
{
}
}
}) { (error) in
}
}
I Hop you Enjoy this tutorial keep touch in this blog for more update
#import <AFNetworking/AFNetworking.h>
#import "WebServiceCallClass.h"
---------------------------------- WebServiceCallClass.h --------------------------
#import <Foundation/Foundation.h>
@interface WebServiceCallClass : NSObject
+(void)callWebServiceUsingGetMethodGetURL:(NSString *)strURL successBlock:(void(^)(id responseObject))sucess failureBlock:(void(^)(id errorResponse))fail;
+(void)callWebServiceUsingPostMethodPostURL:(NSString *)strURL withParamiterDicrionary : (NSDictionary *)paramiterDict successPostBlock:(void(^)(id responseObject))sucess failurePostBlock:(void(^)(id errorResponse))fail;
@end
---------------------------------- WebServiceCallClass.m ----------------------------------
#import "WebServiceCallClass.h"
#import <AFNetworking/AFNetworking.h>
@implementation WebServiceCallClass
+(void)callWebServiceUsingGetMethodGetURL:(NSString *)strURL successBlock:(void(^)(id responseObject))sucess failureBlock:(void(^)(id errorResponse))fail
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
[manager GET:strURL parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
sucess(responseObject);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSData *data = [errResponse dataUsingEncoding:NSUTF8StringEncoding];
id dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dict);
fail(dict);
}];
}
+(void)callWebServiceUsingPostMethodPostURL : (NSString *)strURL withParamiterDicrionary : (NSDictionary *)paramiterDict successPostBlock:(void(^)(id responseObject))sucess failurePostBlock:(void(^)(id errorResponse))fail
{
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
// manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
//If send with header security than open this two line.
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
// [manager.requestSerializer setValue:@"123" forHTTPHeaderField:@"token"];
// [manager.requestSerializer setValue:@"1.0.0" forHTTPHeaderField:@"version"];
[manager POST:strURL parameters:paramiterDict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject)
{
NSError *e = nil;
NSDictionary *responseDict = [NSJSONSerialization
JSONObjectWithData: responseObject
options: NSJSONReadingMutableContainers
error: &e];
sucess(responseDict);
}
failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
{
NSLog(@"%@",error);
NSString* errResponse = [[NSString alloc] initWithData:(NSData *)error.userInfo[AFNetworkingOperationFailingURLResponseDataErrorKey] encoding:NSUTF8StringEncoding];
NSData *data = [errResponse dataUsingEncoding:NSUTF8StringEncoding];
id dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSLog(@"%@",dict);
fail(dict);
}];
}
@end
-------------------------------------- yourmodel.swift ------------------------------
func getdata()
{
let para = NSMutableDictionary()
// para.setValue("1", forKey: "User_ID")
WebServiceCallClass.callWebService(usingPostMethodPostURL: "YOUR.URL.com", withParamiterDicrionary: para as! [AnyHashable : Any], successPost: { (responseData) in
print(responseData)
if let responsestate = (responseData as AnyObject).value(forKey: "response") as? NSDictionary
{
if let responseData = (responsestate).value(forKey: "responseData") as? NSDictionary
{
}
}
}) { (error) in
}
}
I Hop you Enjoy this tutorial keep touch in this blog for more update
Comments
Post a Comment