1

I get below error while trying to return responseString

Incompatible block pointer types sending 'NSString *(^)(NSData *__strong, NSURLResponse *__strong, NSError *__strong)' to parameter of type 'void (^)(NSData *__strong, NSURLResponse *__strong, NSError *__strong)'

where i call it from Suppose AViewController Class

NSString *responseString=[self callAPI];

And below is my code in BViewModel class:

-(NSString* )callAPI
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://appersgroup.com/talkawalk/login.php?email=twalknet@gmail.com&password=123456&latitude=52.486245&longitude=13.327496&device_token=show"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPMethod:@"POST"];
    /* NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
     @"IOS TYPE", @"typemap",
     nil];
     NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
     [request setHTTPBody:postData];
     */

    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        if([responseString rangeOfString:@"nil"].location != NSNotFound)
        {
            NSString * newResponse =  [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

            responseString = newResponse;
        }

        NSLog(@"%@",responseString);
        NSLog(@"response %@",response);
        NSLog(@"error %@",error);

        return responseString;//adding this line give me error ? how to return value 


    }];

    [postDataTask resume];
}
9to5ios
  • 5,319
  • 2
  • 37
  • 65

2 Answers2

2

You can use this method with block like this :

-(void)callAPIWithCompletionHandler : (void (^) (NSString * strResponse)) completionHandler
{
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
    NSURL *url = [NSURL URLWithString:@"http://appersgroup.com/talkawalk/login.php?email=twalknet@gmail.com&password=123456&latitude=52.486245&longitude=13.327496&device_token=show"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
                                                           cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                       timeoutInterval:60.0];

    [request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request addValue:@"application/json" forHTTPHeaderField:@"Accept"];

    [request setHTTPMethod:@"POST"];
    /* NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"TEST IOS", @"name",
     @"IOS TYPE", @"typemap",
     nil];
     NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];
     [request setHTTPBody:postData];
     */

    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

        NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

        if([responseString rangeOfString:@"nil"].location != NSNotFound)
        {
            NSString * newResponse =  [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];

            responseString = newResponse;
        }

        NSLog(@"%@",responseString);
        NSLog(@"response %@",response);
        NSLog(@"error %@",error);

        completionHandler(responseString);

    }];

    [postDataTask resume];
}

Call this method and return response as below :

[self callAPIWithCompletionHandler:^(NSString *strResponse) {

        NSString *responseString = strResponse;
}];

Suppose this method implemented in ViewControllerA, and you want to call this from ViewControllerB.

Then import ViewControllerA in ViewControllerB and make instance of ViewControllerA in ViewControllerB.

ViewControllerA *vcA = [[ViewControllerA alloc] init];
[vcA callAPIWithCompletionHandler:^(NSString *strResponse) {

        NSString *responseString = strResponse;
}];
technerd
  • 14,144
  • 10
  • 61
  • 92
  • please check my edit question, i need to call this API from different class, then how i get the return response in that class ? – 9to5ios Jan 21 '16 at 06:21
  • @iphonemaclover he showed you already. take a look at the last piece of code in his answer. this is how you have to do that – Andrey Chernukha Jan 21 '16 at 06:25
  • i am trying to show output string in textview but its kill it please check NSLog(@"viewcontroller response =%@",responseString); txt_output.text=[NSString stringWithFormat:@"%@",strResponse]; txt_output is in my ViewcontrollerB, and i am returning from ViewcontrollerA NSString *returnstring=[NSString stringWithFormat:@"request=%@ responstring=%@ error=%@",request,responseString,error]; completionHandler(responseString); – 9to5ios Jan 23 '16 at 06:42
  • Please post in question and also post crash log. so i will help you. – technerd Jan 23 '16 at 06:51
  • Please post in question and also post crash log. so i will help you. – technerd Jan 23 '16 at 06:51
  • check question http://stackoverflow.com/questions/34960599/error-showing-response-in-uitextview – 9to5ios Jan 23 '16 at 06:58
1

You are trying to return a string from a block which is not supposed to return any value. This is why the compiler gives you the error. The block will be executed asynchronously, so there's no point in what you're doing. You really need to get familiar with how blocks work, especially when they're executed asynchronously.

Read this: Working with blocks

Andrey Chernukha
  • 21,488
  • 17
  • 97
  • 161