-1

I am executing following code when app is not running after click on "Actionable notification" button.i enabled background mode,remote-notification.

//base url is changed for privacy purpose
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/engine/auth/tx", [SharedData getGatewayURL]]]
                                                                       cachePolicy:NSURLRequestUseProtocolCachePolicy
                                                                   timeoutInterval:HTTP_REQUEST_TIME_OUT];
                [request setHTTPMethod:@"POST"];
                NSString *encodedXML = [encrypted urlEncodeUsingEncoding:NSUTF8StringEncoding];
                NSString *params = [NSString stringWithFormat:@"%@=%@", REQUEST_PARAMETER_NAME, encodedXML];
                [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];


               NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
                NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:[NSOperationQueue mainQueue]];

                NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {


               //Handle response


                  }];
            [postDataTask resume];

//Delegate

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
        if([challenge.protectionSpace.host isEqualToString:@"211.23.34.234"]){
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
        }
    }
}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
    NSData *data = [NSData dataWithContentsOfURL:location];

}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {

}

- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {

}

(I have shown here demo server url not actual)

Can we use NSURLSessionDataTask for executing background task when app is suspended or closed?

This code is perfectly working for Server URL ex. https://211.23.34.234/engine/auth/tx

But when i change Server URL to https://demo.tes.com/engine/auth/tx not working.

Whats wrong with NSURLSessionDataTask ?

Is this due to static IP 211.23.34.234 ? why it same code not working with https://demo.tes.com/engine/auth/tx ? (This server url can change dynamically) any help will much appreciated.

How to fix this?

Note:: setting delegate to "nil" of NSURLSessionDataTask will work for https://demo.tes.com/engine/auth/tx server but not https://211.23.34.234/engine/auth/tx

How to make it work at both server?

Avijit Nagare
  • 7,801
  • 7
  • 34
  • 62

1 Answers1

0

Fixed. issue was in delegate method. which restrict completion handler for demo server.

- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
    if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
  //  if([challenge.protectionSpace.host isEqualToString:@"211.23.34.234"]){    
            NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
            completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
   //     }
    }
}

I was only allowed to auth fixed ip challenge authentication so commenting if condition works fine.

Avijit Nagare
  • 7,801
  • 7
  • 34
  • 62