2016-04-21 17 views
2

自己署名証明書を使用してテストするためにAPIに接続しようとしています。didReirediveメソッドが呼び出されたときにアプリケーションがフリーズする

-(NSData*)getDataFromPostRequestWithHeaders:(NSDictionary*)headers  withPostData:(NSData*)postData fromURL:(NSString*)urlString 
    { 
    __block NSData* responseData; 

    NSLog(@"URL is %@", urlString); 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] 
                  cachePolicy:NSURLRequestUseProtocolCachePolicy 
                 timeoutInterval:10.0]; 
    [request setHTTPMethod:@"POST"]; 
    [request setHTTPBody:postData]; 
    [request setAllHTTPHeaderFields:headers]; 

    NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration]; 
    // [config setHTTPAdditionalHeaders:headers]; 
    NSURLSession *session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil]; 
    NSLog(@"%@", @"NSURLSession started successfully"); 
    // I.e. no I do not need to have a queue of sessions running in parallel currently. 
    NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request 
               completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
              { 
               NSLog(@"%@", @"completionHandler called successfully"); 
                if (error) { 
                 NSLog(@"Error whilst executing post request: %@", error); 
                } else { 
                 NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; 
                 NSLog(@"HTTP response from this request is %@", httpResponse); 
                 responseData = data; 
                } 
               }]; 
    [dataTask resume]; 

    return responseData; 
} 

これは私のdidReceiveChallenge()メソッドです:

-(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler 
{ 
    NSLog(@"%@", @"didReceiveChallenge method of NSURLSessionDelegate called successfully"); 
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) 
    { 
     if ([challenge.protectionSpace.host isEqualToString:@"https://dongu.ravenlabs.co.uk"]) 
     { 
      NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]; 
      completionHandler(NSURLSessionAuthChallengeUseCredential, credential); // I.e. if it is this domain, then yes we can trust it. 
     } 
    } 
} 

最初のものはそうのようなloginメソッド内で呼び出されます。didReceiveChallengeが呼ばれるたび

-(NSString*)loginUserAndRetrieveSessionID:(NSString*)userName withPassword:(NSString*)password 
{ 
    __block NSDictionary *responseDict; 
    NSDictionary *loginHeaders = @{ @"content-type": @"application/json", 
           @"accept": @"application/json", 
           @"cache-control": @"no-cache", 
           }; 
    NSDictionary *parameters = @{ @"login": userName, 
            @"password": password }; 

    NSData *postData = [NSJSONSerialization dataWithJSONObject:parameters options:0 error:nil]; 
    NSString *urlString = [DONGU_API_BASE_URL stringByAppendingString:@"/session/"]; 
    NSData *responseData = [self getDataFromPostRequestWithHeaders:loginHeaders withPostData:postData fromURL:urlString]; 
    if (responseData) 
    { 
    responseDict = [self getDictionaryFromResponseData:responseData]; 
    } 
    NSLog(@"%@", @"End of login method reached."); 
    return [responseDict objectForKey:@"session-token"]; 

} 

、アプリが完全にフリーズします。これを回避する方法はありますか?私はGCDを使ってログインメソッドを呼び出そうとしましたが、喜びはありませんでした。これを回避する方法はありますか?

答えて

0

メソッドが呼び出されるたびに、指定された補完ハンドラを呼び出す必要があります。そうしないと、接続はさらに進まなくなります。特定の課題と何が関係があるのか​​わからない場合は、...PerformDefaultHandlingと呼んでください。

さらに、証明書の検証を無効にする方法は非常に危険であることにご注意ください。最低限、提供された公開鍵と、アプリケーションに格納されている既知の有効な公開鍵をどこかで比較する必要があります。それ以外の場合は、このコードが誤って出荷アプリに入り、TLSのすべての利点を失うリスクがあります。

関連する問題