0

I am trying to cancel an NSURLAuthenticationChanllenge if the server certificate cannot be validated (in my case a self-signed), by popping up an UIAlertView with two buttons -- 'No' and 'Yes'

when user hits Yes, I proceed anyway, but when user hits No, I cancel it. This is how i am doing it. I am using the delegate for the UIAlertView to capture the index of the button that was clicked by the user. When I click Yes, it works fine, but when you click on No, I get an NSRangeException

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

if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
{
    BOOL trusted;
    //Evaluate the cert here -- removed code to be brief

    if(trusted){
        NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
        [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
    }else{
        //define a block
        self.certProceedBlock = ^(NSInteger clickedButton) {
            if(clickedButton == 1){
                NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
                [challenge.sender useCredential:credential forAuthenticationChallenge:challenge];
            }else if(clickedButton == 0){
                [challenge.sender cancelAuthenticationChallenge:challenge]; //this fails with NSRangeException
            }
        };


        //pop up the Notification
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Unknown Certificate"
                                                        message:[NSString stringWithFormat:message, certificateSubjectName]
                                                       delegate:self
                                              cancelButtonTitle:@"No"
                                              otherButtonTitles:@"Yes", nil];
        [alert show];


    }
}

}

Here is the delegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(self.certProceedBlock){
    self.certProceedBlock(buttonIndex);
}

}

The error is: * Terminating app due to uncaught exception 'NSRangeException', reason: '* -[__NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]'

As requested-- here is the full stackTrace

i tried to add the image of the stack trace -- but the image is too small. here is the text. please copy into textEdit-- it should be better

[NSArrayI objectAtIndex:]: index 1 beyond bounds [0 .. 0]' *** First throw call stack: ( 0 CoreFoundation 0x004e5946 __exceptionPreprocess + 182 1 libobjc.A.dylib 0x02bd2a97 objc_exception_throw + 44 2 CoreFoundation 0x003c8bd2 -[__NSArrayI objectAtIndex:] + 210 3 MYCODE** 0x0017d9e1 -[HttpUtils getAuthTokenExpiration:] + 145 4 MYCODE** 0x0017dfb3 -[HttpUtils saveAuthTokenExpirationDate:] + 99 5 MYCODE** 0x0017ed5a __68-[HttpUtils getToken:params:username:password:tokenCompletionBlock:]_block_invoke + 218 6 MYCODE** 0x001850ce -[HttpUtils connection:didFailWithError:] + 334 7 CFNetwork 0x0497d38e __67-[NSURLConnectionInternalConnection cancelAuthenticationChallenge:]_block_invoke + 51 8 CFNetwork 0x04995879 __65-[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:]_block_invoke + 83 9 CFNetwork 0x0497cdd9 -[NSURLConnectionInternalConnection invokeForDelegate:] + 145 10 CFNetwork 0x04995813 -[NSURLConnectionInternal _withConnectionAndDelegate:onlyActive:] + 189 11 CFNetwork 0x04995a10 -[NSURLConnectionInternal _withConnectionAndDelegate:] + 58 12 CFNetwork 0x0497d314 -[NSURLConnectionInternalConnection cancelAuthenticationChallenge:] + 384 13 CFNetwork 0x048c100d -[NSURLConnection(NSURLAuthenticationChallengeSender) cancelAuthenticationChallenge:] + 50 14 MYCODE** 0x00184b82 __58-[HttpUtils connection:didReceiveAuthenticationChallenge:]_block_invoke + 370 15 MYCODE** 0x00184f52 -[HttpUtils alertView:clickedButtonAtIndex:] + 178 16 UIKit 0x0186960d -[UIAlertView _prepareToDismissForTappedIndex:] + 192 17 UIKit 0x01868fa9 __35-[UIAlertView _prepareAlertActions]_block_invoke50 + 56 18 UIKit 0x018618b5 -[UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:] + 72 19 UIKit 0x01861868 -[UIAlertController _dismissAnimated:triggeringAction:] + 56 20 UIKit 0x01861454 -[UIAlertController _actionViewTapped:] + 68 21 libobjc.A.dylib 0x02be8771 -[NSObject performSelector:withObject:] + 70 22 UIKit 0x01a4474c -[_UIAlertControllerActionView _triggerSelect] + 60 23 UIKit 0x01a44607 -[_UIAlertControllerActionView touchesEnded:withEvent:] + 187 24 UIKit 0x019e47b7 _UIGestureRecognizerUpdate + 13225 25 UIKit 0x015fb26b -[UIWindow _sendGesturesForEvent:] + 1356 26 UIKit 0x015fc0cf -[UIWindow sendEvent:] + 769 27 UIKit 0x015c1549 -[UIApplication sendEvent:] + 242 28 UIKit 0x015d137e _UIApplicationHandleEventFromQueueEvent + 20690 29 UIKit 0x015a5b19 _UIApplicationHandleEventQueue + 2206 30 CoreFoundation 0x004091df __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 15 31 CoreFoundation 0x003feced __CFRunLoopDoSources0 + 253 32 CoreFoundation 0x003fe248 __CFRunLoopRun + 952 33 CoreFoundation 0x003fdbcb CFRunLoopRunSpecific + 443 34 CoreFoundation 0x003fd9fb CFRunLoopRunInMode + 123 35 GraphicsServices 0x0697324f GSEventRunModal + 192 36 GraphicsServices 0x0697308c GSEventRun + 104 37 UIKit 0x015a98b6 UIApplicationMain + 1526 38 MYCODE** 0x001f857d main + 141 39 libdyld.dylib 0x03349ac9 start + 1 ) libc++abi.dylib: terminating with uncaught exception of type NSException

** removed my app name and replaced with string MYCODE

4

0 回答 0