ios - 我们可以忽略 NSMutableURLRequest 中的 ssl 验证吗?

标签 ios objective-c validation ssl

我对 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/

相关文章:

ios - 比较 alamofire 返回的错误对象

objective-c - 10.5 上未触发鼠标拖动事件

ios - Realm - cocoa : How to add notification inside other notification block

validation - 如何在 Go 中处理控制台输入类型验证?

ios - 在没有 iTunes 的情况下将音乐和有声读物的文件夹复制到 iPhone(并且不必先标记我的所有媒体)

ios - 在 iOS 包中搜索全文的工具

ios - 有什么方法可以检测用户是否实际按下了应用程序中的 Twitter/Facebook 发布按钮?

ios - 如何使嵌入式 segue 响应用户操作

c# - 如果 ASP.NET MVC 表单已失效,如何保留字段值?

forms - Laravel:多个文件上传,Input::hasFile(key) 始终为 false