1

I have a NSURLconnection implementation and am trying to migrate to NSURlsession for IOS-9.

NSURLconnection implementation:

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

Custom delegate methods:

-(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];

Now when i modified the above impl to NSURLsession based impl, i now have

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

Custom delegate Method :

- (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]; 

After this , i am getting compilation error as below:

./../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...

I am not able to understand why delegate=self in NSURLSession Impl is throwing such an error? SmHttpsAuthenticationHandler is an interface defined as

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

Please help.Please give any pointers on NSURLSession implementation. Thanks a lot.

Community
  • 1
  • 1
CodeTry
  • 312
  • 1
  • 19

1 Answers1

0

In your interface declaration, you need to declare that your class conforms to the NSURLSessionDelegate protocol, e.g.

@interface SmHttpsAuthenticationHandler : NSObject

dgatwood
  • 10,129
  • 1
  • 28
  • 49
  • yes , you are correct . I did that and it worked .@interface SmHttpsAuthenticationHandler : NSObject – CodeTry Sep 22 '15 at 10:25