如果无法验证服务器证书(在我的情况下是自签名的),我试图取消NSURLAuthenticationChanllenge,通过弹出带有两个按钮的UIAlertView - ' No'并且'是'
当用户点击是时,我仍然继续,但是当用户点击否时,我取消它。这就是我这样做的方式。我正在使用UIAlertView的委托来捕获用户单击的按钮的索引。当我单击是时,它工作正常,但当您单击否时,我得到一个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];
}
}
}
这是代表
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if(self.certProceedBlock){
self.certProceedBlock(buttonIndex);
}
}
错误是: *由于未捕获的异常终止应用' NSRangeException',原因:' * - [__ NSArrayI objectAtIndex:]:索引1超出边界[0 .. 0]&#39 ;
根据要求 - 这是完整的stackTrace
我试图添加堆栈跟踪的图像 - 但图像太小。 这是文字。请复制到textEdit--它应该更好
[ NSArrayI objectAtIndex:]:索引1超出边界[0 .. 0]' ***第一次抛出调用堆栈: ( 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连接: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:以NSException类型的未捕获异常终止
**删除了我的应用名称并替换为字符串MYCODE