我正在使用Objective-C。我们知道AFNetworking正在另一个线程中工作,但如果我想在互联网请求后做某事,该怎么办呢。
例如:
- (BOOL)checkPassword
{
self.loginPermit = NO;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSDictionary *parameters = @{@"username": self.username, @"password": self.password};
[manager POST:self.URL parameters:parameters progress:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
NSDictionary *dict = (NSDictionary *)responseObject;
if ([[dict objectForKey:@"msg"] isEqualToString:@"00001"]){
self.loginPermit = YES
}
else {
self.loginPermit = NO;
}
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error);
}];
return self.loginPermit;
}
但每次都会返回NO。如何在Internet请求后返回值。
答案 0 :(得分:0)
你应该使用阻止。
[self checkPassword:^(BOOL loginPermit) {
if(loginPermit){
// do your stuff
}
}];
- (void)checkPassword:(void(^)(BOOL loginPermit))completion
{
BOOL loginPermit = NO;
// show indicator
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
NSDictionary *parameters = @{@"username": self.username, @"password": self.password};
[manager POST:self.URL parameters:parameters progress:nil
success:^(NSURLSessionDataTask *task, id responseObject)
{
//hide indicator
NSDictionary *dict = (NSDictionary *)responseObject;
if ([[dict objectForKey:@"msg"] isEqualToString:@"00001"]){
loginPermit = YES;
}
else {
loginPermit = NO;
}
completion (loginPermit);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
NSLog(@"Error: %@", error);
//hide indicator
completion (loginPermit);
}];
}