首页 > 解决方案 > 无法让 WebKit 显示网页

问题描述

我正在尝试研究如何在我的 IOS 应用程序中显示网页。我试图仔细遵循我能找到的信息。这是我正在使用的:

@implementation FirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.productURL = @"http://google.com/";

    NSURL *url = [NSURL URLWithString:self.productURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    _webView = [[WKWebView alloc] initWithFrame:self.view.frame];
    [_webView loadRequest:request];
    _webView.frame = CGRectMake(self.view.frame.origin.x+50,self.view.frame.origin.y+50, self.view.frame.size.width-100, self.view.frame.size.height-200);
    printf("\nReady to add the Subview");
    self.view.backgroundColor = [UIColor blueColor];
    [self.view addSubview:_webView];
    printf("\nSubView has been added");
 }

@end

框架在上面是偏移的,所以我可以给它上色并看到子视图将被定位在那里。据我了解,该网页应该出现在子视图中,但我只是得到一个空白页面。

我还将 NSExceptionDomains 字典添加到我的 Info.plist 并添加了 google.com,其中包含一个字典,其中 NSIncludesSubdomains 设置为 true,NSExceptionAllowsInsecureHTTPLoads 设置为 true,认为这可能是问题所在,但无济于事。我已经尝试了几个网址和同样的事情。

我怎样才能找出正在发生的事情并解决它?

标签: iosobjective-cwebkitmobile-webkit

解决方案


我发现了问题!显然,我需要将键“App Transport Security Settings”作为字典添加到我的 Info.plist 中,并将键“Allow Arbitrary Loads”添加为 true。它现在显示!

我以前找不到这个,但是当我找到并添加它时,它与上面的代码一样工作得很好。


推荐阅读