-1

I'm new to iOS while I'm developing I am calling web services every where...I want like one single class for get,post,put methods...then call [self post]; [parameters:....]... that means I want to call single methods for all get services and post.. please help me...how it is..

NSURL *url = [NSURL URLWithString:@"https://example.com/"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];

NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:
                        height, @"user[height]",
                        weight, @"user[weight]",
                        nil];
[httpClient postPath:@"/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *responseStr = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
    NSLog(@"Request Successful, response '%@'", responseStr);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"[HTTPClient Error]: %@", error.localizedDescription);
}];

For AFNetworking 2.0 (and also using the new NSDictionary syntax):

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *params = @{@"user[height]": height,
                         @"user[weight]": weight};
[manager POST:@"https://example.com/myobject" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
Razib
  • 10,965
  • 11
  • 53
  • 80
NaniBhai
  • 3
  • 3

3 Answers3

1

From here you can create singleton class - How to create singleton class in objective C

Then you need to create your own method with completion handler and call where you want.

Example: Post Method:

+(void)postWebserviceWithURL:(NSString*)webServiceURL withParam:(NSDictionary*)urlParameters withCompletion:(void(^)(NSDictionary*response))completion {
//Your code goes here
}
Community
  • 1
  • 1
Sohil R. Memon
  • 9,404
  • 1
  • 31
  • 57
0

here is the related answer.You should create nsobject classes something like webservices and create the singleton instance object like this.

+(WebServices *)sharedInstance{

    /* Use this to make it a singleton class */
    if (sharedObj==Nil) {
        sharedObj=[[WebServices alloc]init];
    }
    return sharedObj;
     /**/
}

using this singleton instance you can call the methods what ever you required.

Eg: [[webservices sharedInstance] post];

With in the method you can use the required web services hits such as post,get and put.

use the Nsnotifiers to consume the responses from the API hits.

Mahesh Babu
  • 3,395
  • 9
  • 48
  • 97
0
+ (instancetype)sharedInstance
{
static CustomClass *sharedInstance = nil;
static dispatch_once_t onceToken;

dispatch_once(&onceToken, ^{

//Override your constructor for custom initialization if you want
    sharedInstance = [[CustomClass alloc] init];
});

return sharedInstance;
}

Then define your methods for CRUD actions.

simply access like

[[CustomClass sharedInstance] POST:...];
[[CustomClass sharedInstance] GET:...];
erenkabakci
  • 442
  • 4
  • 15