gpt4 book ai didi

ios - 如何监控来自WKWebView的http请求?

转载 作者:行者123 更新时间:2023-11-29 00:04:04 25 4
gpt4 key购买 nike

所以,我认为这是一个非常明确的问题。 This question是关于监控ajax请求。我试过了,但没有按我的意愿工作。

每次发送、接收请求以及在 WKWebView 中更改浏览链接时,如何在我的 native iOS Obj-C 应用程序中调用一个函数?

最佳答案

好的,我找到方法了。

您可以创建一个新类(我们称它为 MyURLProtocol),它具有 NSURLProtocol作为子类。然后将此函数添加到 MyURLProtocol:

+ (BOOL)canInitWithRequest:(NSURLRequest *)request {
NSLog(@"URL = %@", request.URL.absoluteString);
return NO;
}

每次您的 webview 发出请求时都会调用此函数。然后你需要在加载系统中注册这个协议(protocol)。在您的 Appdelegate.m 文件中包含您的类并添加/替换 didFinishLaunchingWithOptions 函数:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[NSURLProtocol registerClass:[MyURLProtocol class]];
return YES;
}

一切就绪。现在您可以编辑 canInitWithRequest 函数并对请求执行您想要的操作。

关于ios - 如何监控来自WKWebView的http请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48623516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com