0

私はこの問題を何時間もグーグルで調べてきましたが、解決策が見つからないようです。

私が従った最後のチュートリアルは、信頼されていない SSL 接続 (この場合は自己署名証明書) を介して JSON 要求を送信する方法を説明するこのチュートリアルでした。

要するに、これまでに試したコードは次のとおりです。

このメソッドは、トークンが Web API で有効かどうかを確認します。

-(BOOL)linked
{
    if([self token] == nil)
    {
        return NO;
    }else
    {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@&object=User&action=check_authorized", SPAtajosApiLink, [self token], nil]];
        NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
        [req setHTTPMethod:@"POST"];
        NSLog(@"%@", req);
        [req setHTTPBody: [mandatoryParameters dataUsingEncoding: NSUTF8StringEncoding]];

        NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:req delegate:self];
        [connection start];

        NSDictionary *validity = [NSJSONSerialization
                                  JSONObjectWithData:[NSURLConnection sendSynchronousRequest:req returningResponse:nil error:nil]
                                  options:NSJSONReadingMutableContainers
                                  error:nil];
        NSLog(@"RESPONSE LINKED %@ %@", validity[@"code"], validity[@"message"]);
    }
    return YES;
}

そして、次のように NSURLConnectionDelegate メソッドを実装しました。

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        NSLog(@"API LINK %@", challenge.protectionSpace.host);
        if ([challenge.protectionSpace.host isEqualToString:SPAtajosApiLink])
        {
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        }
    }

    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

それでも、私のアプリは常に次のメッセージでクラッシュします。

2013-10-15 22:48:55.382 Atajos[733:60b] { URL: https://MY_API_URL/www/?token=657fd6c02b3ba439e69b060f7f7680cc&object=User&action=check_authorized } 2013-10-15 22:48:58.045 Atajos[733:3b17] NSURLConnection/CFURLConnection HTTP ロードに失敗しました (kCFStreamErrorDomainSSL、-9813) 2013- 10-15 22:48:58.049 Atajos[733:60b] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'data parameter is nil' * First throw call stack: (0x2ec2de8b 0x38f286c7 0x2ec2ddcd 0x2f5b5d77 0x5df9d 0x62395 0x31433025 0x3141e631 0x313b8be7 0x313b7edd 0x3141dca1 0x3389976d 0x33899357 0x2ebf877f 0x2ebf871b 0x2ebf6ee7 0x2eb61541 0x2eb61323 0x3141cf43 0x314181NSe5 0x62191 0x39421ab7) 例外:

さて、NSData、おそらく NSJSONSerialization のパラメーターは NULL です。質問、なぜですか?

奇妙なことに、信頼されていない SSL を介して Web サイトと対話する UIWebView で実際に動作する同様のコードがあります。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    //Need to rebuild base URL. Otherwise I get the parameters which will always fail the check.
    NSURL *requestUrl = [request URL];
    NSString *basicPathString = [NSString stringWithFormat:@"%@://%@:%@%@", requestUrl.scheme, requestUrl.host, requestUrl.port, requestUrl.path];
    NSLog(@"%@", basicPathString);

    if([basicPathString isEqualToString:SPAtajosLoginLink])
    {

        __block NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:request.URL.absoluteURL]
                                                                             options:NSJSONReadingMutableContainers
                                                                               error:nil];
        NSLog(@"Auth token is %@", jsonResponse[@"token"]);
        [self dismissViewControllerAnimated:YES completion:^{
            if([jsonResponse[@"code"] isEqualToNumber:[NSNumber numberWithInt:200]])
            {
                [[PDKeychainBindings sharedKeychainBindings] setObject:jsonResponse[@"token"]
                                                                forKey:@"com.shortcutpublicity.ios.atajos.userAccessToken"];
            }else
            {
                UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"No Se Pudo Autenticar", @"Error 500 title")
                message:NSLocalizedString(@"Se ha producido un error interno del servidor (500). Por favor, trata de nuevo más tarde.", @"Error describing error 500")
                delegate:nil
                cancelButtonTitle:NSLocalizedString(@"Aceptar", @"Accept") otherButtonTitles:nil];
                [alertView show];
            }
         }];
    }else {
    //--------------THIS IS THE RELEVANT PART------------
        BOOL result = _Authenticated;
        if (!_Authenticated)
        {
            _FailedRequest = request;
            NSURLConnection *trash = [[NSURLConnection alloc] initWithRequest:request delegate:self];
            if(trash)
            {
                NSLog(@"Allocated succesfully.");
            }
        }
        return result;
    }
    return YES;
}

-(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        NSURL* baseURL = [NSURL URLWithString:SPatajosAuthLink];
        if ([challenge.protectionSpace.host isEqualToString:baseURL.host]) {
            NSLog(@"trusting connection to host %@", challenge.protectionSpace.host);
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        } else
            NSLog(@"Not trusting connection to host %@", challenge.protectionSpace.host);
    }
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)pResponse {
    _Authenticated = YES;
    [connection cancel];
    [_webView loadRequest:_FailedRequest];
}

私は WebView で動作するこのコードを適応させようとしましたが、NSURLConnection で使用するように適応させることができませんでした。

正直なところ、これらの回避策がどのように機能するかはよくわかりません。私は他人のコードを盗み、適応させようと試みてきましたが、役に立ちませんでした。NSURLConnection と NSURLRequest のドキュメントを見てきましたが、これらの問題を解決できませんでした。私を助けてください。

4

2 に答える 2

1

テスト用に #ifedf ブロックでこのカテゴリを使用します...これを本番環境に残さないでください:

#ifdef DEBUG
@interface NSURLRequest (IgnoreSSL)
   +(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host;
@end
@implementation NSURLRequest (IgnoreSSL)
   +(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host {return YES;}
@end
#endif
于 2013-10-17T19:18:43.863 に答える
0
try this Delegate method    




 - (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {



        //    SecTrustRef trustRef = [[challenge protectionSpace] serverTrust];
        //    SecTrustEvaluate(trustRef, NULL);
        //
        //    SecCertificateRef certRef = SecTrustGetCertificateAtIndex(trustRef, 0);
        //
        //    NSData *certificateData = (__bridge NSData *) SecCertificateCopyData(certRef);
        //    NSLog(@"Subject: %@", [[NSString alloc] initWithData:certificateData encoding:NSUTF8StringEncoding]);

        // [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];


        SecTrustRef trust = challenge.protectionSpace.serverTrust;
        NSURLCredential *cred;
        cred = [NSURLCredential credentialForTrust:trust];
        [challenge.sender useCredential:cred forAuthenticationChallenge:challenge];



    }
于 2013-10-16T09:23:18.123 に答える