我对 IOS 开发非常陌生,我想知道是否可以忽略“NSMutableURLRequest”类的 ssl 验证。如果是的话请指导我。我通过重写忽略了通过委托(delegate)进行的 SLL 验证
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;
我希望在没有委托(delegate)的情况下忽略 SLL 验证。
我的代码片段
NSString *username = _userName.text;
NSString *password = _password.text;
//HTTP Basic Authentication
NSString *authenticationString = [NSString stringWithFormat:@"%@:%@", username, password];
NSData *authenticationData = [authenticationString dataUsingEncoding:NSUTF8StringEncoding];
NSString *authenticationValue = [authenticationData base64Encoding];
//Set up your request
NSMutableURLRequest *request1 = [[NSMutableURLRequest alloc] initWithURL:url];
// Set your user login credentials
[request1 setValue:[NSString stringWithFormat:@"Basic %@", authenticationValue] forHTTPHeaderField:@"Authorization"];
// Send your request asynchronously
[NSURLConnection sendAsynchronousRequest:request1 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *responseCode, NSData *responseData, NSError *responseError) {
if ([responseData length] > 0 && responseError == nil){
//logic here
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"RESPONSE WITHOUT DELEGATE: %@",newStr);
[[ApplicationStorage applicationStorage] setUserName:_userName.text];
[[ApplicationStorage applicationStorage] setUserName:_password.text];
[[ApplicationStorage applicationStorage] setUserName:_serverAdd.text];
}}];
谢谢
阿米斯
最佳答案
我认为这个问题之前已经得到了回答。您可以尝试将其添加到您的 AppDelegate.m
中,位于 @end
#if DEBUG
@implementation NSMutableURLRequest (NSURLRequestWithIgnoreSSL)
+ (BOOL)allowsAnyHTTPSCertificateForHost:(NSString *)host
{
return YES;
}
@end
#endif
关于ios - 我们可以忽略 NSMutableURLRequest 中的 ssl 验证吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31075013/