AFNetworking成功与失败称之为未成功

时间:2015-04-08 13:12:11

标签: ios objective-c afnetworking

我在这里有这个方法:

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

我没有错误,但是当我把刹车点放在成功和失败上时,他们都没有被击中。我知道网址是正确的,我可以看到传入的信用卡,我不知道问题是什么?

更新

我已将代码更改为:

-(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) {

        }
    }];

我想要返回true还是false,我的Login方法仍然有用吗?

更新

我已将此方法更新为:

-(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')

我接近了吗?

1 个答案:

答案 0 :(得分:1)

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

  • 将闭包更改为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