0
我想創建一個函數,它將根據AFNetworking POST請求的響應返回一個值。基於AFNetworking響應的返回值
但是,我不能找出一種方法來做到這一點,因爲函數是異步的,所以在實際收到響應之前返回值。
int didLogin;
__block NSString *response;
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSDictionary *parameters = @{@"username":username, @"password":password};
[manager POST:loginUrl parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
//json "response" object from server response
response = [responseObject objectForKey:@"response"];
NSLog(@"Response: %@", response);
[HUD hide:YES];
[HUD removeFromSuperViewOnHide];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
[HUD hide:YES];
[HUD removeFromSuperViewOnHide];
NSLog(@"Login Error: %@", error);
}];
if ([response compare:@"1"] == NSOrderedSame || [response compare:@"2"] == NSOrderedSame)
{
//successful login/1-new device/2-existing device
didLogin = 1;
}
else if ([response compare:@"0"] == NSOrderedSame)
{
//unsuccessful login/invalid password
didLogin = 0;
}
else{
//unsuccessful login
didLogin = 2;
}
在哪一點它將返回didLogin。
有什麼辦法可以使這項工作,還是我需要使用同步請求?
花了一個小時弄清楚如何設置回調,但我想我已經完成了所有工作。謝謝! – Justin