Strange errors while getting UIWebView to display content

My project has a web view embedded into a view controller, and an external HTML file.

I'm trying to get my web view to load the contents of the html file, which I've successfully done until it suddenly stopped and started spewing this into the output window:


2017-03-04 21:01:19.923841 TestApp[807:166552] doing will appear
2017-03-04 21:01:20.095647 TestApp[807:166552] did will appear
2017-03-04 21:01:23.045754 TestApp[807:166612] WF: === Starting WebFilter logging for process TestApp
2017-03-04 21:01:23.046555 TestApp[807:166612] WF: _userSettingsForUser mobile: {
    filterBlacklist =     (
    );
    filterWhitelist =     (
    );
    restrictWeb = 1;
    useContentFilter = 0;
    useContentFilterOverrides = 0;
    whitelistEnabled = 0;
}
2017-03-04 21:01:23.046995 TestApp[807:166612] WF: _WebFilterIsActive returning: NO


After a bit of googling and documentation research, I found that the most common reason for this is Apple Transport Security. But ATS can't be the issue, because even after setting NSAllowsArbitraryLoads to YES, I'm still getting the same output. Not to mention the fact that it was working for a while before adding ATS keys to my plist.


So what else could cause this to occur?


Here's my code:

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"doing will appear");

    CGRect screenRect = [[UIScreen mainScreen] bounds];
    self.webView = [[UIWebView alloc] initWithFrame:screenRect];
    self.webView.delegate = self;
    self.webView.allowsInlineMediaPlayback = YES;

    NSString *htmlString = @"<html><head/><body><iframe src=\"http://www.google.com\" width=\"320\" height=\"568\" frameborder=\"0\" scrolling=\"yes\" allowfullscreen=\"true\"></iframe></body></html>";
    [self.webView loadHTMLString:htmlString baseURL:nil];

    NSLog(@"did will appear");
}

Replies

HTML file version:

- (void)viewWillAppear:(BOOL)animated
{
    NSLog(@"doing will appear");
   
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    self.webView = [[UIWebView alloc] initWithFrame:screenRect];
    self.webView.delegate = self;
    self.webView.allowsInlineMediaPlayback = YES;
   
    NSString *path = [[NSBundle mainBundle] bundlePath];
    NSString *indexPath = [NSBundle pathForResource:@"index" ofType:@"html" inDirectory:path];
    [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:indexPath]]];

    NSLog(@"did will appear");
}

If I create a new app from the Single View Application template and replace the contents of ViewControlle with this:

- (UIWebView *)webView {
    return (UIWebView *) self.view;
}

- (void)loadView {
    self.view = [[UIWebView alloc] initWithFrame:CGRectZero]; 
    self.webView.delegate = self; 
    self.webView.allowsInlineMediaPlayback = YES; 

    NSString *htmlString = @"<h1>Hello Cruel World!</h1>"; 
    [self.webView loadHTMLString:htmlString baseURL:nil]; 
}

it displays my HTML just fine.

I’m not entirely sure what’s going on with your app but I suspect there’s combination of issues:

  • I couldn’t get your

    htmlString
    to display anything useful. I didn’t investigate why not, but I suspect there’s a problem with the actual HTML.
  • You’d be better off loading

    <http://www.google.com>
    , to avoid any App Transport Security entanglements.
  • Loading your view in

    -viewWillAppear:
    is a very bad idea; use
    -loadView
    , like I show above.
  • If you’re setting up the main view in a view controller you don’t need to get the screen bounds; the enclosing context will resize your view appropriately, so you can just pass in

    CGRectZero
    .
  • The “_userSettingsForUser” log you’re seeing looks like some sort of restriction (in the Settings > General > Restrictions sense). You might want to start with a new test app, like I described above, to see if that has the same problem.

OH, and why are you using the not-quite-deprecated-but-still-old UIWebView, rather than the shiny new WKWebView?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I, too, have discovered this new log entry, and cannot find anyway to fix or suppress it. It used to Just Work.


>OH, and why are you using the not-quite-deprecated-but-still-old UIWebView, rather than the shiny new WKWebView?

That's easy: WEKWebView doesn't support POST.