-
项目中找到.entitlements,在这里里添加键值对com.apple.security.network.client Boolean 1
-
错误提示, 如果不设置权限,会一直调webViewWebContentProcessDidTerminate,报错
2022-04-11 18:33:56.334398+0800 MacOS[12981:7896625] [] [0x7fa76184d420] CVCGDisplayLink::setCurrentDisplay: 0
2022-04-11 18:33:56.335376+0800 MacOS[12981:7896625] [] [0x7fa76184d400] CVDisplayLinkCreateWithCGDisplays count: 1 [displayID[0]: 0x0] [CVCGDisplayLink: 0x7fa76184d420]
2022-04-11 18:33:56.355331+0800 MacOS[12981:7896625] [] [0x7fa76184e020] CVCGDisplayLink::setCurrentDisplay: 69734406
2022-04-11 18:33:56.355434+0800 MacOS[12981:7896625] [] [0x7fa76184e000] CVDisplayLinkCreateWithCGDisplays count: 1 [displayID[0]: 0x4281006] [CVCGDisplayLink: 0x7fa76184e020]
2022-04-11 18:33:56.442141+0800 MacOS[12981:7896625] [Process] 0x1102f2600 - [PID=0] WebProcessProxy::didFinishLaunching: Invalid connection identifier (web process failed to launch)
2022-04-11 18:33:56.442195+0800 MacOS[12981:7896625] [Process] 0x1102f2600 - [PID=0] WebProcessProxy::processDidTerminateOrFailedToLaunch: reason=4
2022-04-11 18:33:56.442342+0800 MacOS[12981:7896625] [Process] 0x7fa75f830020 - [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::processDidTerminate: (pid 0), reason 4
2022-04-11 18:33:56.444266+0800 MacOS[12981:7896625] [Loading] 0x7fa75f830020 - [pageProxyID=5, webPageID=6, PID=0] WebPageProxy::dispatchProcessDidTerminate: reason=4
macos WKWebView用法
- swift
//
// ViewController.swift
// MacOS
//
// Created by LN on 2022/4/1.
//
import Cocoa
import WebKit
class ViewController: NSViewController, WKNavigationDelegate {
private var request : URLRequest?
private var webView: WKWebView?
override func viewDidLoad() {
super.viewDidLoad()
webView = WKWebView.init(frame: self.view.frame)
webView!.navigationDelegate = self
webView!.allowsBackForwardNavigationGestures = true
self.view.addSubview(self.webView!)
self.webView!.mas_makeConstraints { make in
make?.edges.equalTo()(self.view)
}
self.setupData()
}
func setupData(){
let html = "http://www.baidu.com"
let url = URL.init(string: html)!
self.request = URLRequest.init(url: url)
if self.webView != nil {
self.webView!.load(self.request!)
}
}
override var representedObject: Any? {
didSet {
// Update the view, if already loaded.
}
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
}
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
}
}
- oc
#import "ViewController.h"
#import <WebKit/WebKit.h>
@interface ViewController ()
@property (nonatomic,strong)WKWebView * webView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//观察窗口拉伸
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenResize)
name:NSWindowDidResizeNotification
object:nil];
//初始化
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
//加载出来
[self.view addSubview:_webView];
//1.网络
_webView.allowsBackForwardNavigationGestures = YES;
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.hao123.com/"]];
[_webView loadRequest:request];
}
-(void)screenResize{
NSLog(@"观察窗口拉伸");
NSLog(@"%.2f===%.2f",self.view.bounds.size.width,self.view.bounds.size.height);
_webView.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height);
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
其他报错,添加完环境后报错
You can disable this error by setting 'CODE_SIGN_ALLOW_ENTITLEMENTS_MODIFICATION' to 'YES'
- 解决方法:只需要在Xcode中的Build Setting里添加User-Defined Setting,命名为
CODE_SIGN_ALLOW_ENTITLEMENTS_MODIFICATION
然后把值设定为YES即可解决
20210220223825893.png
网友评论