Make a synchronous HTTP call with RestKit

前端 未结 1 1139
逝去的感伤
逝去的感伤 2020-12-12 05:35

When logging a user into my application I need to pull a user object down from the server using only the username. This returns the userId (among other things) that I need i

1条回答
  •  离开以前
    2020-12-12 06:26

    Putting aside the question of whether this is the ideal design for an iPhone application, I was able to accomplish what I was hoping using blocks.

    [[RKObjectManager sharedManager] loadObjectsAtResourcePath:[@"/api/users/" stringByAppendingString:[_userNameField text]] usingBlock:^(RKObjectLoader* loader) {
    
        loader.onDidLoadResponse = ^(RKResponse *response) {
    
            NSLog(@"Response: \n%@", [response bodyAsString]);
        };
    
        loader.onDidLoadObjects = ^(NSArray *objects) {
    
            APIUser *apiUser = [objects objectAtIndex:0];
            NSLog(@"user_id is %i", apiUser.user_id);
    
    
        };
    
        loader.onDidFailWithError = ^(NSError *error) {
    
    
                UIAlertView *badLoginAlert = [[UIAlertView alloc]initWithTitle:NSLocalizedString(@"LOGIN_FAILED", nil)
                                                                       message:NSLocalizedString(@"BAD PASSWORD OR USERNAME", nil)
                                                                      delegate:self
                                                             cancelButtonTitle:NSLocalizedString(@"OK", nil)
                                                             otherButtonTitles:nil];
                [badLoginAlert show];           
        };
    }];
    

    Hope this helps someone.

    0 讨论(0)
提交回复
热议问题