user979331
user979331

Reputation: 11851

AFNetworking Incompatible block pointer types sending

I am using AFNetworking to access a URL with Windows Authentication. I was using ASIHTTPRequest to login like so:

-(BOOL)User:(NSString *)user andPassWordExists:(NSString *)password
{
    NSURL *url = [NSURL URLWithString:kIP];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setUseSessionPersistence:YES];
    [request setUseKeychainPersistence:NO];
    [request setUsername:user];
    [request setPassword:password];
    [request setDomain:@"domain"];
    [request startSynchronous];

    NSError *loginError = [request error];
    if(loginError == nil){
        return true;
    }else{
        return false;
    }

}

and now I am trying to do the same thing with AFNetworking and this what I came up with from this example: http://www.raywenderlich.com/forums/viewtopic.php?f=2&t=18385

-(BOOL)User:(NSString *)user andPassWordExists:(NSString *)password
{
    NSURL *url = [NSURL URLWithString:kIP];

    NSURLRequest *request = [NSURLRequest requestWithURL:url
                                             cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
                                         timeoutInterval:90.0];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];
    [operation setCredential:[NSURLCredential credentialWithUser:[@"domain" stringByAppendingString:user]
                                                        password:password persistence:NSURLCredentialPersistenceNone]];
    operation.responseSerializer = [AFJSONResponseSerializer serializer];


    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        return true;

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        return false;

    }];
    [operation start];
}

but this gives me two errors:

/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/Classes/LHJSonData.m:148:46: Incompatible block pointer types sending 'int (^)(AFHTTPRequestOperation *__strong, __strong id)' to parameter of type 'void (^)(AFHTTPRequestOperation *__strong, __strong id)'

/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/Classes/LHJSonData.m:152:15: Incompatible block pointer types sending 'int (^)(AFHTTPRequestOperation *__strong, NSError *__strong)' to parameter of type 'void (^)(AFHTTPRequestOperation *__strong, NSError *__strong)'

How Can I create a method that will use user and password to login and store those credentials...this is possible with AFNetworking, it sure was with ASIHTTPRequest

Upvotes: 1

Views: 1222

Answers (1)

Mike D
Mike D

Reputation: 4946

I think you are misunderstanding blocks. You are returning TRUE/FALSE from the callback blocks, not your method. Those blocks will be executed at some point in the future, and their return type is void. Try running this code to see the order:

-(void)User:(NSString *)user andPassWordExists:(NSString *)password
{
    // construct your request

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"In success block");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"In Fail block");
    }];
    [operation start];

    NSLog(@"Network op started");
}

As to your design, I would not return true or false, you have to find another way to manage the success and fail scenarios, which could look something like this (I don't know enough about your overall design to give a definitive answer):

-(void)User:(NSString *)user andPassWordExists:(NSString *)password
{
    // construct your request

    __weak typeof(self) weakSelf = self;
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        // called at somepoint in the future if the request is success full
        [weakSelf handleSuccess:responseObject];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // called at somepoint in the future if the request fails
        [weakSelf handleFail:error];
    }];
    [operation start];
}

- (void)handleSuccess(id)response {
    // process the response
}

- (void) handleFail:(NSError*)error {
    // evaluate error
}

You see the bock declaration here: https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking/AFHTTPRequestOperation.h

Upvotes: 2

Related Questions