我有一个子类:
#import <Foundation/Foundation.h>
@interface CustomURLConnection : NSURLConnection <NSURLConnectionDelegate>
@end
在它的实现文件中,我具有以下功能:
-(void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSLog(@"Authenticating from subclass.");
}
请注意,这didReceiveAuthenticationChallenge
是NSURLConnectionDelegate
这个片段目前在每个发送NSURLRequest
using的类中NSURLConnection
:
-(void) connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
// Code to authenticate ourselves.
}
实际问题:
我希望子类具有预先确定的行为
connection:(CustomURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
而不必在每个类中实现该功能。而是让每个类都使用子类,并让子类自动处理每个身份验证挑战。
班级分配如下:
cUrlConnection = [[CustomURLConnection alloc]initWithRequest:req delegate:self startImmediately: YES];
if (cUrlConnection)
{
// Handle events when connection is active.
}
如果有人对我如何CustomURLConnection
处理身份验证机制和/或提示/指针有任何见解,我会很高兴。