0
    NSURLCredential *credential2 = [NSURLCredential
                  credentialForTrust:challenge.protectionSpace.serverTrust
                  credentialWithUser: @"username"
                                            password: @"password"
                                         persistence:NSURLCredentialPersistenceNone];


    [challenge.sender useCredential:credential2 forAuthenticationChallenge:challenge];

我需要绕过带有 UIWebView 的自签名证书,同时还能够传入用户名和密码进行身份验证。

 NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];

NSURLCredential *credential = [NSURLCredential credentialWithUser:@"username" 密码:@"password" persistence:NSURLCredentialPersistenceNone];

两者都是单独有效的,当我尝试将它们结合起来时,它们都会大惊小怪......有什么想法吗?

4

1 回答 1

0

使用 RestKit 和下面的代码以及用于登录凭据的 alertView 来解决此问题。

- (id)initWithURL:(NSURL*)pageURL {

    _retainedURL = pageURL;
    [_retainedURL retain];

    RKClient *client;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    client = [RKClient clientWithBaseURL: pageURL];
    client.username=[userDefaults objectForKey:@"UserName"];
    client.password=[userDefaults objectForKey:@"Password"];
    client.authenticationType=RKRequestAuthenticationTypeHTTPBasic;
    client.disableCertificateValidation= TRUE;
    RKRequest *request=[RKRequest requestWithURL:pageURL delegate:self];
    [client configureRequest:request];
    request.authenticationType = RKRequestAuthenticationTypeHTTPBasic;
    request.disableCertificateValidation = TRUE;
    self.authenticatedRequest = request;
    [request sendAsynchronously];

    if(self = [super init]) {
        self.URL = pageURL;
        self.availableActions = SVWebViewControllerAvailableActionsOpenInSafari | SVWebViewControllerAvailableActionsMailLink | SVWebViewControllerAvailableActionsCopyLink;
    }
    return self;
}

登录 alertView 将用户名和密码的文本添加到 userdefaults 并将保留的 URL 传递回 initWithURL 函数。当页面未通过身份验证(使用登录凭据)时,将调用登录警报视图。

于 2012-11-20T19:30:53.370 回答