WKWebView在后台,几个奇怪的断言 [英] WKWebView in background, several strange assertion

查看:1800
本文介绍了WKWebView在后台,几个奇怪的断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找出iOS中当前浏览器的userAgent.

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

因此,在Xcode创建的默认项目中,我添加了:

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,例如Mac OS X)AppleWebKit/605.1.15(KHTML,例如Gecko)移动设备/15E148 :(空)

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

但是之后它会打印:

test4 [1930:101220]用法:< WKWebView:0x7f958f817800;框架=(0 0; 00);层=< CALayer:0x60000236ed00>>

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

test4 [1930:101220] [assertion]获取断言时出错:< Error域= RBSAssertionErrorDomain代码= 3"目标未运行或缺少所需的目标权利".UserInfo = {RBSAssertionAttribute =< RBSmainAttribute |域:"com.apple.webkit"名称:背景"sourceEnvironment:(null)",NSLocalizedFailureReason =目标不是运行或缺少所需的目标权利}>

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:无法获取RBS后台断言'WebProcess后台断言'用于使用PID 1933的过程,错误:错误域= RBSAssertionErrorDomain代码= 3"目标未运行或缺少所需的目标权利".UserInfo = {RBSAssertionAttribute =< RBSmainAttribute |域:"com.apple.webkit"名称:背景"sourceEnvironment:(null)",NSLocalizedFailureReason =目标不是运行中或缺少所需的目标权利}

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}

这看起来不太好.我的代码有问题吗,是否有可能避免这些断言而仍然获取userAgent字符串?

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

推荐答案

创建一个空的 UIView 并将 WKWebView 添加为子视图.

Create an empty UIView and add WKWebView as a subview.

这篇关于WKWebView在后台,几个奇怪的断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆