我有一个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];现在,当我将上面的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实现上给出任何指示。非常感谢。
发布于 2015-09-22 07:29:20
在您的接口声明中,您需要声明您的类符合NSURLSessionDelegate协议,例如
@接口SmHttpsAuthenticationHandler : NSObject
https://stackoverflow.com/questions/32650990
复制相似问题