4

I am trying to show a WKWebView over another view using the presentViewController method but only shows a white screen.

I have implemented the WKWebView in my ViewController.m as follows:

- (void)viewDidLoad {
  [super viewDidLoad];
  WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
  _webView = [[WKWebView alloc] initWithFrame:self.view.frame
                                 configuration:configuration];
  _webView.navigationDelegate = self;
  [_webView loadRequest:[NSURLRequest requestWithURL:url]];
  [self.view addSubview:_webView];
}

then in another class I have a method with the following code:

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
ViewController *webView1 =[[ViewController alloc] init];

[delegate.window.rootViewController presentViewController: webView1 
                                                 animated:YES completion:nil]; 

This last code "presents" a white screen on top of my rootViewController. I have no clue of why my webView is a white screen.

Andres C. Viesca
  • 332
  • 5
  • 19

2 Answers2

1

What URL are you loading?

It might be that you need to add to your plist file permissions to load a non-secure URL.

Add this to your plist and see if that works.

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

For more info, see here: https://ooiks.com/2017/10/15/ios-wkwebview-return-a-blank-screen-white-screen/

Also make sure that the URL you are trying uses HTTP or HTTPS. Make sure that UIApplication.shared.canOpenURL(url : URL(string: yourURLString)) returns true for your URL.

Joshua Wolff
  • 2,687
  • 1
  • 25
  • 42
-2

So, I found why it was just showing a white screen. Turns out that you have to wrap the WKWebView with a UINavigationController.

By adding:

UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController: _webView];

to my custom method in my custom class and then present the controller I just created instead of the _webView:

 [delegate.window.rootViewController presentViewController: controller animated:YES completion:nil];

Works fine now.

Andres C. Viesca
  • 332
  • 5
  • 19
  • You don't "have to wrap the WKWebview" with a UINavigationController. Mine is working without a navigation controller. – ScottyBlades Apr 08 '22 at 21:46