user979331
user979331

Reputation: 11851

AFNetworking Success and Failure called not being made

I have this method here:

-(BOOL)User:(NSString *)user andPassWordExists:(NSString *)password
{


   __block BOOL isLoggedIn = YES;
   NSURL *url = [NSURL URLWithString:kIP];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];

    [operation setCredential:[NSURLCredential credentialWithUser:[@"domain" stringByAppendingString:user]
                                                        password:password persistence:NSURLCredentialPersistenceForSession]];



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


        isLoggedIn = YES;


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

        isLoggedIn = NO;

    }];


    [operation start];
    return isLoggedIn;
}

I get no errors, but when I put brake points on success and failure and neither one of them are getting hit. I know the URL is correct and I can see the creds being passed in, I dont know what the issue is ?

UPDATE

I have changed my code to this:

-(void)User:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler
{


   NSURL *url = [NSURL URLWithString:kIP];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];

    [operation setCredential:[NSURLCredential credentialWithUser:[@"domain" stringByAppendingString:user]
                                                        password:password persistence:NSURLCredentialPersistenceForSession]];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [[NSOperationQueue mainQueue] addOperation:operation];

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


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


    }];


    [operation start];
}

Now I need to add this is my code:

[self fetchDataWithCompletionHandler:^(id responseObject, NSError *error) {
    if (responseObject) {

    }
}];

but when I do, I get this error:

Use of undeclared identifier 'self'

I guess this where I am confused because this how I would call this method from another method:

- (void)Login
{
    NSString *rawString = [self.idTextField text];
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    [self.idTextField setText:[rawString stringByTrimmingCharactersInSet:whitespace]];
    BOOL *isAuthenticated = [userName User:self.idTextField.text andPassWordExists:self.passwordTextField.text];
    if(isAuthenticated != 0){

        [self.idTextField removeFromSuperview];
        [self.passwordTextField removeFromSuperview];
        [self.loginButton removeFromSuperview];
        self.idTextField = nil;
        self.passwordTextField = nil;
        self.loginButton = nil;
        [self CreateMenu];
    }else{
        [self CustomAlert:@"Sorry Login Failed, User and/or Passsword Incorrect"];
    }

    [indicatorView stopAnimating];
    [indicatorView removeFromSuperview];
    indicatorView = nil;
    [loadingView removeFromSuperview];
    loadingView = nil;

}

So inside this:

[self fetchDataWithCompletionHandler:^(id responseObject, NSError *error) {
        if (responseObject) {

        }
    }];

would I want to either return true or false and my Login method would still work?

UPDATE

I have updated my method to this:

-(void)User:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler
{


   NSURL *url = [NSURL URLWithString:kIP];
   NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
                                         initWithRequest:request];

    [operation setCredential:[NSURLCredential credentialWithUser:[@"domain" stringByAppendingString:user]
                                                        password:password persistence:NSURLCredentialPersistenceForSession]];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [[NSOperationQueue mainQueue] addOperation:operation];

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

        if (completionHandler) {
            completionHandler(responseObject, nil);
        }

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

        if (completionHandler) {
            completionHandler(nil, error);
        }

    }];


    [operation start];

    [self User:^(id responseObject, NSError *error) {
        if (responseObject) {
            return true;
        }
    }];
}

Two things wrong here....

I get an error at the end of my fetchDataWithCompletionHandler code:

/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/Classes/LHJSonData.m:237:5: Control may reach end of non-void block

and I get this warning:

/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/Classes/LHJSonData.m:233:11: Instance method '-fetchDataWithCompletionHandler:' not found (return type defaults to 'id')

Am I getting close?

Upvotes: 1

Views: 1132

Answers (1)

Rob
Rob

Reputation: 437432

This is an excellent start. I might suggest

  • changing the closure to simply be BOOL;

  • eliminate starting the operation until you're done configuring everything (and either call start or add it to a queue, but not both); and

  • eliminate the call to user at the end.

Thus

- (void)verifyUserid:(NSString *)user password:(NSString *)password completionHandler:(void (^)(BOOL success, NSError *error))completionHandler
{
    NSURL *url = [NSURL URLWithString:kIP];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    [operation setCredential:[NSURLCredential credentialWithUser:[@"domain" stringByAppendingString:user]
                                                        password:password persistence:NSURLCredentialPersistenceForSession]];

    operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (completionHandler) {
            completionHandler(TRUE, nil);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if (completionHandler) {
            completionHandler(FALSE, error);
        }
    }];

    [operation start];
}

I've also changed the method name to conform to Cocoa conventions (start with verb, method names always begin with lowercase letter), but that's more a stylistic matter than anything else.

Now you can call this method with something like:

[self verifyUserid:user password:password completionHandler:^(BOOL success, NSError* error) {
    if (success) {
         // userid/password ok; do anything related to successful login here
    } else {
         // not ok
    }
}];

// note, we're not logged in yet (the above completion block is called later),
// so don't assume we're logged in or not here

Upvotes: 1

Related Questions