【问题标题】:Not able to use custom delegate in NSURLSession无法在 NSURLSession 中使用自定义委托
【发布时间】:2015-09-18 11:31:59
【问题描述】:

我有一个 NSURLconnection 实现,并正在尝试迁移到 IOS-9 的 NSURlsession。

NSURL 连接实现:

@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 中的 delegate=self 会抛出这样的错误? SmHttpsAuthenticationHandler 是一个接口定义为

 @interface SmHttpsAuthenticationHandler: NSObject{....} and then   @implementation SmHttpsAuthenticationHandler

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

【问题讨论】:

    标签: ios objective-c delegates nsurlconnection nsurlsession


    【解决方案1】:

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

    @interface SmHttpsAuthenticationHandler : NSObject

    【讨论】:

    • 是的,你是对的。我这样做了,它起作用了 .@interface SmHttpsAuthenticationHandler : NSObject
    猜你喜欢
    • 2014-02-07
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多