xiubojin / JXBWKWebView

An component WebView for iOS base on WKWebView
MIT License
736 stars 115 forks source link

不能正确加载需要身份认证的网页 #7

Closed ghost closed 5 years ago

ghost commented 5 years ago

如图所示:

abc

使用Safari加载相同网页会有弹窗:

324

xiubojin commented 5 years ago

这个不是WebView的问题,建议让你们server好好检查一下代码和配置

ghost commented 5 years ago

🤔🤔🤔老哥你好,我把Safari加载相同网页的截图发给你看,就是为了说明这个网站用其他浏览器打开是正常的。。

xiubojin commented 5 years ago

才打开邮箱,请问问题解决了吗。

金修博

17610158997@163.com | 签名由网易邮箱大师定制

在2018年11月24日 20:42,keep foolish?notifications@github.com 写道:

🤔🤔🤔老哥你好,我把Safari加载相同网页的截图发给你看,就是为了说明这个网站用其他浏览器打开是正常的。。

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub, or mute the thread.

ghost commented 5 years ago

嗯嗯,可以了

`- (void)webView:(WKWebView )webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge )challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential ))completionHandler { NSString hostName = webView.URL.host;

NSString *authenticationMethod = [[challenge protectionSpace] authenticationMethod];
if ([authenticationMethod isEqualToString:NSURLAuthenticationMethodDefault]
    || [authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPBasic]
    || [authenticationMethod isEqualToString:NSURLAuthenticationMethodHTTPDigest]) {
    NSString *title = @"网页认证";
    NSString *message = [NSString stringWithFormat:@"%@ 需要用户名和密码", hostName];
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"用户名";
    }];
    [alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
        textField.placeholder = @"密码";
        textField.secureTextEntry = YES;
    }];
    [alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {

        NSString *userName = ((UITextField *)alertController.textFields[0]).text;
        NSString *password = ((UITextField *)alertController.textFields[1]).text;

        NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:userName password:password persistence:NSURLCredentialPersistenceNone];

        completionHandler(NSURLSessionAuthChallengeUseCredential, credential);

    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
    }]];
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:alertController animated:YES completion:^{}];
    });
}
else if ([authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
    // needs this handling on iOS 9
    completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
    // or, see also http://qiita.com/niwatako/items/9ae602cb173625b4530a#%E3%82%B5%E3%83%B3%E3%83%97%E3%83%AB%E3%82%B3%E3%83%BC%E3%83%89
}
else {
    completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
}

}`