无法在NSURLSession中使用自定义委托

时间:2015-09-18 11:31:59

标签: ios objective-c delegates nsurlconnection nsurlsession

我有一个NSURLconnection实现,我正在尝试迁移到NSOSlsession for IOS-9。

NSURLconnection实施:

@property (nonatomic, strong, readwrite) NSURLConnection *  connection;    

自定义委托方法:

-(BOOL)connection:(NSURLConnection *)connection ---canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)protectionSpace   
-(void)connection:(NSURLConnection *)connection  didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge  
- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)aresponse  
-(void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data  
- (void)connectionDidFinishLoading:(NSURLConnection *)conn
- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError*)error  

self.connection = [NSURLConnection connectionWithRequest:request delegate:self];

现在,当我将上述impl修改为基于NSURLsession的impl时,我现在已经

@property (nonatomic, strong, readwrite) NSURLSession *session;  

自定义委托方法:

- (BOOL)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace*)protectionSpace  
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge  
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveResponse:(NSURLResponse *)aresponse  
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveData:(NSData *)data   
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task connectionDidFinishLoading:(NSURLConnection *)conn  
- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didFailWithError:(NSError *)error  


NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration    defaultSessionConfiguration];
self.session = [NSURLSession sessionWithConfiguration:configuration                                                                  delegate:self                                                                    delegateQueue: nil];
NSURLSessionDataTask *task = [self.session dataTaskWithRequest:request];
[task resume]; 

在此之后,我收到编译错误如下:

./../source/smmacosx/smplt.m:663:33: error: sending     'SmHttpsAuthenticationHandler *' to parameter of incompatible type 'id<NSURLSessionDelegate> _Nullable' [-Werror]

                                                                                                                           delegate: self

                                                                                                                                   ^~~~

 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS9.0.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSURLSession.h:135:132: note: 

      passing argument to parameter 'delegate' here

 + (NSURLSession *)sessionWithConfiguration:(NSURLSessionConfiguration   *)configuration delegate:(nullable id <NSURLSessionDelegate>)delegate delegateQueue:(nullable NSOperati...

我无法理解为什么委托=自我在NSURLSession中Impl会抛出这样的错误? SmHttpsAuthenticationHandler是一个定义为

的接口
 @interface SmHttpsAuthenticationHandler: NSObject{....} and then   @implementation SmHttpsAuthenticationHandler

请帮助。请提供有关NSURLSession实施的任何指示。 非常感谢。

1 个答案:

答案 0 :(得分:0)

在您的接口声明中,您需要声明您的类符合NSURLSessionDelegate协议,例如

@interface SmHttpsAuthenticationHandler:NSObject