我正在使用AFNetworking,代码运行良好,但在成功连接后没有调用NSURLConnectionDelegate方法的委托方法。
我已经在我的接口文件中声明为NSURLConnection的委托。但似乎没有任何工作,是否与使用后台线程和主线程有关? 这是我的代码:
NSLog(@"\n\n1 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));
AFHTTPRequestOperationManager *manager=[AFHTTPRequestOperationManager manager];
manager.responseSerializer.acceptableContentTypes= [NSSet setWithObject:@"text/html"];
NSDictionary *parameters = @{
@"email": [Constants sharedInstance].email,
@"password": [Constants sharedInstance].password,
};
NSLog(@"\n\nparameters: %@",parameters);
[manager POST:@"https://anyurl/" parameters:parameters success:^
(AFHTTPRequestOperation *operation, id responseObject) {
// [operation setRunLoopModes:[NSSet setWithObject:NSDefaultRunLoopMode]];
NSLog(@"\n\n 2 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));
[self stopLoading];
NSLog(@"\n\nconnection successful!!");
NSLog(@"\n\nJSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"\n\nError: %@", error);
[self stopLoading];
NSLog(@"Connection could not be made");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No Network" message:@"Cannot make connection to the server" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
}
];
NSLog(@"\n\n 3 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));
[self startLoading];
NSLog(@"\n\n4 Is main thread %@", ([NSThread isMainThread] ? @"Yes" : @" NOT"));
我看到了link,但无法使用该答案。
答案 0 :(得分:0)
AFNetworking
是一个单独的库,如果您使用AFNetworking
,它的成功和失败块将被执行。如果您要执行NSURLConnectionDelegate
方法,请使用NSURLConnection
代替AFNetwork