UIWebView不显示我的网页

对于一些背景信息,我试图展示的网页是一个当前在AWS的EC2上托pipe的Web应用程序。 后端是Python w / Flask,前端只是简单的HTML / CSS。 该URL具有HTTP,因为它尚未使用HTTPS进行保护。 当这个网页的URL被打开时,浏览器要求的第一件事是login凭证(浏览器询问,而不是网站)。 这个页面在我的iPhone上的移动版Safari 加载,而且Safari成功地请求了凭据。 如果我input正确,它将正确加载页面。

所以我已经尝试在应用传输安全设置下允许任意负载以及使用以下按键的自定义的例外域:

App Transport Security Settings Dictionary Exception Domains Dictionary my website URL Dictionary NSIncludesSubdomains Boolean (YES) NSExceptionAllowsInsecureHTTPLoads Boolean (YES) NSThirdPartyExceptionAllowsInsecureHTTPLoads Boolean (YES) NSExceptionMinimumTLSVersion String (TLSv1.0) NSExceptionRequiresForwardSecrecy Boolean (YES) 

然而,每当我在模拟器上启动应用程序,所有我回来是一个白色的屏幕(如果需要可以张贴截图)。

这是我在ViewController.swift中的代码:

 import UIKit class ViewController: UIViewController { @IBOutlet var WebView: UIWebView! override func viewDidLoad() { super.viewDidLoad() let url = NSURL(string: "My URL inserted here") let request = NSURLRequest(URL: url!) WebView.loadRequest(request) } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

如果我使用允许任意负载,当我在输出框中查看时,它不会说“ App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.当我正确configuration例外域(允许任意负载删除)它也不会给我这个消息。 有时候,当我使用exception域来更改设置(再次,允许任意负载删除)时,我是否会得到这个输出。

我开始认为这个问题超出了安全范围,任何意见或步骤,我可以采取尝试解决这个问题将不胜感激,谢谢!

白屏有点奇怪,假设一个401会导致一个标准的错误页面,但也许服务器为此设置了一个白页。 我的猜测是直接在URL中设置用户名和密码是行不通的,反正你不应该那样做,而是依赖于WKWebViewwebView:didReceiveAuthenticationChallenge: WKWebView webView:didReceiveAuthenticationChallenge:委托方法。

这里有一些示例代码希望工作/帮助:

 #import "ViewController.h" @import WebKit; @interface ViewController () <WKNavigationDelegate> @property (nonatomic, strong) WKWebView *webView; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; self.webView = [[WKWebView alloc] initWithFrame:self.view.frame configuration:[WKWebViewConfiguration new]]; self.webView.navigationDelegate = self; [self.view addSubview:self.webView]; [self.webView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_webView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_webView)]]; [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_webView]-0-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(_webView)]]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; NSURL *target = [NSURL URLWithString:@"http://yourhost.com/possiblePage.html"]; NSURLRequest *request = [NSURLRequest requestWithURL:target]; [self.webView loadRequest:request]; } - (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler { NSURLCredential *creds = [[NSURLCredential alloc] initWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession]; completionHandler(NSURLSessionAuthChallengeUseCredential, creds); } @end 

这基本上是一个简单的ViewController的实现文件(就像XCode的单个视图模板一样)。 它也向你展示了如何添加一个WKWebView 。 绝对要确保检查出所有的委托方法,这样你就知道这个东西能为你做什么。

显然,密码和用户名必须以某种方式设置,我想你可以使用一个简单的警报popup窗口让用户input这个信息(这原则上类似于Safari)。 对于第一个testing,你可以硬编码。 另外请注意,我在那里设置了一个示例子页面,只需使用通常在桌面浏览器上使用的完全相同的URL即可。 哦,而且由于服务器没有SSL, 你需要允许任意的加载