Segev Segev - 6 months ago 76
iOS Question

UIWebView shows a black screen with no content

I've created a UIWebView from the IB and subclassed it to my needs. Somewhere inside my app I'm using the following code:

_mainWebView = nil;
_mainWebView = [[MyWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];
_mainWebView.delegate = self;
NSURL *url = [NSURL URLWithString:@""];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_mainWebView loadRequest:request];
NSLog(@"WebView: %@", _mainWebView); //->> WebView: <MyWebView: 0x15403000; baseClass = UIWebView; frame = (0 0; 320 548); layer = <CALayer: 0x1544dac0>>

Which gives me a black screen with no content.


It's most likely because you're setting _mainWebView to nil. This pretty much means you "unplug" the outlet you hooked up from Interface Builder. So the web view you're configuring in this code isn't connected to the screen at all. There are two options you can choose from:

1) Use Interface Builder, connect the outlets, and remove the first two lines of code in the snippet above.

2) Remove the web view from Interface Builder, remove the first line of code in the snippet above, and add [self.view addSubview:_mainWebView]; before you load the request.

By the way, I believe UIWebView is not to be subclassed, at least in an App Store release. It could be potentially breakable in future iOS's...

I hope this helps you!