AFNetworking Success 和 Failure 称为未创建

AFNetworking Success and Failure called not being made

我这里有这个方法:

-(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;
}

我没有出错,但是当我在成功和失败上设置制动点时,它们都没有被击中。我知道 URL 是正确的,我可以看到正在传递的凭据,我不知道问题是什么?

更新

我已将我的代码更改为:

-(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];
}

现在我需要添加这是我的代码:

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

    }
}];

但是当我这样做时,我得到这个错误:

Use of undeclared identifier 'self'

我猜这是我感到困惑的地方,因为这是我从另一个方法调用此方法的方式:

- (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;

}

所以在里面:

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

        }
    }];

我想要 return true 还是 false 并且我的登录方法仍然有效?

更新

我已经将我的方法更新为:

-(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;
        }
    }];
}

这里有两处错误....

我在 fetchDataWithCompletionHandler 代码末尾遇到错误:

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

我收到此警告:

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

我接近了吗?

这是一个很好的开始。我可能会建议

  • 将闭包更改为 BOOL;

  • 消除开始操作,直到您完成所有配置(并调用 start 或将其添加到队列中,但不能同时进行);和

  • 去掉最后对user的调用

因此

- (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];
}

我还更改了方法名称以符合 Cocoa 约定(以动词开头,方法名称始终以小写字母开头),但这更像是一种文体问题。

现在您可以使用类似以下内容调用此方法:

[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