3

您好,提前感谢您的帮助。我一直在寻找,但找不到答案。我只为 iOS 编程一周。

到目前为止,与 Web 服务的所有连接都正常工作,我已经创建了一个类和方法来执行这些调用。我正在进行标准登录,用户输入登录信息,应用程序将这些值传递给 Web 服务,它返回一个布尔值,具体取决于信息是否与数据库中的任何内容匹配。之后,应用程序获取返回值并移动到下一个屏幕或显示错误警报。或者至少这就是我的目标。

问题是,在进行其余调用或解析响应之前执行条件,并且我没有太多运气找到解决方案。我读过异步和同步调用,但在实现它们时运气不佳。

这是调用代码:

//Class that has the methods for calling the web services
__restObj  = [[restCalls alloc] init];
bool login = [__restObj restLogin:user passwd:pass];
if (login) {
   [self performSegueWithIdentifier:@"adminLogin" sender:self];
}
else{
   UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Incorrect group name or password." delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
   [alert show];
}

在实际 POST 发生之前执行的条件总是错误的。

这是我的方法:

- (bool)restLogin:(NSString*)user passwd:(NSString*)pass{
// Load the object model via RestKit
RKObjectManager *objectManager = [RKObjectManager sharedManager];

groupInfo *gi = [[groupInfo alloc] init];

gi.gName = user;
gi.pass = pass;


RKObjectMapping *userInfoMapping = [RKObjectMapping requestMapping];
[userInfoMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}];

RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userInfoMapping
                                                                               objectClass:[groupInfo class]
                                                                               rootKeyPath:nil];
[objectManager addRequestDescriptor:requestDescriptor];
objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

[objectManager postObject:gi
                     path:@"adminLoginIos"
               parameters:nil
                  success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                      NSArray* statuses = [mappingResult array];
                      NSLog(@"Loaded statuses: %@", statuses);
                      _result = [statuses objectAtIndex:0];
                  }
                  failure:^(RKObjectRequestOperation *operation, NSError *error) {
                      NSLog(@"Hit error: %@", error);
                  }
 ];

return _result;
}

在此先感谢并请记住,我对此很陌生,因此感谢您的帮助,如果我的代码不是最好的,请告诉我。

问候, ChmlGr

4

1 回答 1

2

您需要传入一个块,然后在成功回调中,块返回_result。

基于您的结构的示例如下:

-(void) restLogin:(NSString*)user passwd:(NSString*)pass block:(void (^)(id))block {
    // Load the object model via RestKit
    RKObjectManager *objectManager = [RKObjectManager sharedManager];

    groupInfo *gi = [[groupInfo alloc] init];

    gi.gName = user;
    gi.pass = pass;


    RKObjectMapping *userInfoMapping = [RKObjectMapping requestMapping];
    [userInfoMapping addAttributeMappingsFromDictionary:@{@"gName": @"groupName",@"pass":@"pass"}];

    RKRequestDescriptor *requestDescriptor = [RKRequestDescriptor requestDescriptorWithMapping:userInfoMapping
                                                                                   objectClass:[groupInfo class]
                                                                                   rootKeyPath:nil];
    [objectManager addRequestDescriptor:requestDescriptor];
    objectManager.requestSerializationMIMEType = RKMIMETypeJSON;

    [objectManager postObject:gi
                         path:@"adminLoginIos"
                   parameters:nil
                      success:^(RKObjectRequestOperation *operation, RKMappingResult *mappingResult) {
                          NSArray* statuses = [mappingResult array];
                          NSLog(@"Loaded statuses: %@", statuses);
                          _result = [statuses objectAtIndex:0];
                          block(_result);
                      }
                      failure:^(RKObjectRequestOperation *operation, NSError *error) {
                          NSLog(@"Hit error: %@", error);
                          block(nil);
                      }
     ];

}

您对该方法的调用类似于:

[__restObj restLogin:user passwd:pass block:^(id obj) {
    // do something here to translate that object into a BOOL and check value    
}];

我不使用 RestKit,所以我无法验证这正是你需要的,但它应该能让你走上正确的道路。也就是说,如果您想查看 AFNetworking,我编写了一个 NetworkClient 包装器,我不介意分享。

于 2013-03-06T16:42:17.617 回答