[英]Not able to use custom delegate in NSURLSession
我有一个NSURLconnection实现,正在尝试迁移到IOS-9的NSURlsession。
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];
现在,当我将上述暗示修改为基于NSURLsession的暗示时,我现在有了
@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中的proxy = self会抛出这样的错误? SmHttpsAuthenticationHandler是一个定义为
@interface SmHttpsAuthenticationHandler: NSObject{....} and then @implementation SmHttpsAuthenticationHandler
请帮忙。请提供有关NSURLSession实现的任何指针。 非常感谢。
在接口声明中,您需要声明您的类符合NSURLSessionDelegate协议,例如
@interface SmHttpsAuthenticationHandler:NSObject
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.