WKWebView in background, several strange assertion

Viewed 10156

I want to find out userAgent of current browser in iOS.

So in default project created by Xcode I added:

#import "ViewController.h"
#import <WebKit/WKWebView.h>

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    dispatch_async(dispatch_get_main_queue(), ^{
        WKWebView *obj = [[WKWebView alloc] initWithFrame:CGRectZero];
        [obj evaluateJavaScript: @"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
            NSLog(@"evaluateJavaScript = %@ : %@", result, error);
            NSLog(@"To make sure that webview alive: %@", obj);
        }];
    });
}


@end

it prints what I expect:

evaluateJavaScript = Mozilla/5.0 (iPad; CPU OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 : (null)

but after that it prints:

test4[1930:101220] usage: <WKWebView: 0x7f958f817800; frame = (0 0; 0 0); layer = <CALayer: 0x60000236ed00>>

test4[1930:101220] [assertion] Error acquiring assertion: <Error Domain=RBSAssertionErrorDomain Code=3 "Target is not running or required target entitlement is missing" UserInfo={RBSAssertionAttribute=<RBSDomainAttribute| domain:"com.apple.webkit" name:"Background" sourceEnvironment:"(null)">, NSLocalizedFailureReason=Target is not running or required target entitlement is missing}>

test4[1930:101220] [ProcessSuspension] 0x102dfed00 - ProcessAssertion: Failed to acquire RBS Background assertion 'WebProcess Background Assertion' for process with PID 1933, error: Error Domain=RBSAssertionErrorDomain Code=3 "Target is not running or required target entitlement is missing" UserInfo={RBSAssertionAttribute=<RBSDomainAttribute| domain:"com.apple.webkit" name:"Background" sourceEnvironment:"(null)">, NSLocalizedFailureReason=Target is not running or required target entitlement is missing}

This doesn't look good. Is something wrong with my code, is it possible to avoid these assertions and still get userAgent string?

1 Answers

Create an empty UIView and add WKWebView as a subview.

Related