Import ApiCall Model object class in ViewController. h
Call delegate APIDelegate in ViewController. h to define the APICall delegate methods in our ViewController View
//=== 
    UILabel  * lbl ;
#import <UIKit/UIKit. h>
#import "ViewController. h"
#import "APICall. h"
@interface ViewController  ()<APIDelegate>
{
}
@end
//=== "ViewCont ro ller . M"
-(void ) viewDidAppear : (BOOL) animated
{
    [super  viewDidAppear : animated];
    APICall * api= [[APICall alloc] init];
    api.del = self;
    [api makeAPICallWithMethodName:@"greeting" methodTyep:@"GET" body:nil];
}
-(void ) serviceSuccess : (NSData *) data methodName : (NSString *) method
{
    NSLog( @"Data :  %@", data);
    NSLog( @"%@", jsonDict );
    dispatch_async( dispatch_get_main_queue( ), ^{ 
    });
}
-(void ) serviceError : (NSError *) error methodName : (NSString *) method{ 
}
//=== All the Magic is in APICall. h model objects to call the service calls
#import <Foundation/Foundation. h>
#define BASEURL @"http: //rest-service. guides. spring. io /"
@protocol APIDelegate
@optional
-(void ) serviceSuccess : (NSData *) data methodName : (NSString *) method;
-(void ) serviceError : (NSError *) error methodName : (NSString *) method;
@end
@interface APICall :  NSObject
{
}
@property (strong, nonatomic ) id<APIDelegate> del ;
-(void ) makeAPICallWithMethodName : (NSString *) methodName  methodTyep : (NSString *) methodType  body: (NSData *) body;
@end
//=== APICAll . M
#import "APICall. h"
@implementation APICall 
-(NSMutableURLRequest *)prepareRequestWithMethodName:(NSString *)methodName methodType:(NSString *)methodType body:(NSData *)bodyData
{
                               @"postman-token": @"b559e00e-c5a5-e96e-63ea-40907d757a39", @"AUTHTOKEN": @"djajkdhakj " };
    NSString * urlString = [NSString stringWithFormat:@"%@%@",BASEURL,methodName];
                                                       timeoutInterval:10.0];
    [request  setHTTPMethod : methodType ];
    [request  setAllHTTPHeaderFields : headers];
        [request  setHTTPBody : bodyData ];
    }
}
-(void ) conncectWithServerRequest : (NSURLRequest *) request methodName : (NSString *) method
{
                                                        NSLog( @"%@", error);
                                                        [self.del serviceError : error methodName : method];
                                                    } else  {
                                                        NSLog( @"%@", httpResponse );
                                                        [self.del serviceSuccess : data methodName : method];
                                                    }
                                                }];
    [dataTask  resume ];
}
-(void ) makeAPICallWithMethodName : (NSString *) methodName  methodTyep : (NSString *) methodType  body: (NSData *) body
{
    [self  conncectWithServerRequest : request methodName : methodName ];
}
@end
No comments:
Post a Comment