2016-07-26 16 views
-1

Ich führe folgenden Code, wenn App ist nicht läuft nach Klicken auf "Actionable Benachrichtigung" button.i aktiviert Hintergrund-Modus, Remote-Benachrichtigung.NSURLSessionDataTask Aufruf funktioniert nicht in objective c?

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

// Delegierter

- (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 { 

} 

(Ich habe hier Demo-Server gezeigt URL nicht unbedingt)

Können wir NSURLSessionDataTask verwenden zum Ausführen von Hintergrundaufgabe, wenn app suspendiert oder geschlossen ?

Dieser Code funktioniert perfekt für Server URL ex. https://211.23.34.234/engine/auth/tx

Aber wenn ich ändern Server URL-https://demo.tes.com/engine/auth/tx nicht funktioniert.

Was ist falsch mit NSURLSessionDataTask?

Liegt das an statische IP 211.23.34.234? Warum funktioniert der gleiche Code nicht mit https://demo.tes.com/engine/auth/tx? (Diese Server-URL kann dynamisch geändert werden) Jede Hilfe wird sehr geschätzt.

Wie behebt man das?

Hinweis:: Einstellung Delegierten auf „Null“ von NSURLSessionDataTask für https://demo.tes.com/engine/auth/tx Server arbeiten, aber nicht https://211.23.34.234/engine/auth/tx

Wie es an beiden Server funktioniert?

+0

Was sind die Antwortdaten, die Sie erhalten? – Suresh

+0

Keine Antwort vom Server. Es ist im Hintergrund, als die App geschlossen wurde. –

+0

@RonakChaniyara. Link ist eine Demo, die nicht zum Datenschutz angezeigt wird. –

Antwort

0

Fest. Problem war in Delegate-Methode. welche den Abschluss-Handler für den Demo-Server einschränken.

- (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); 
    //  } 
    } 
} 

Ich durfte nur feste IP-Challenge-Authentifizierung Authentifizierung so kommentieren, wenn die Bedingung gut funktioniert.

Verwandte Themen