How can i monitor requests on WKWebview?

如何在WKWebview上监控请求?

I'v tried using NSURLprotocol (canInitWithRequest) but it won't monitor ajax requests (XHR), only navigation requests(document requests)

我尝试过使用NSURLprotocol(canInitWithRequest)但它不会监视ajax请求(XHR),只监视导航请求(文档请求)

3 个解决方案

#1


28

Finally I solved it

最后我解决了

Since I don't have control over the web view content, I injected to the WKWebview a java script that include a jQuery AJAX request listener.

由于我无法控制Web视图内容,因此我向WKWebview注入了一个包含jQuery AJAX请求侦听器的java脚本。

When the listener catches a request it sends the native app the request body in the method:

当侦听器捕获请求时,它会在方法中向本机应用程序发送请求正文:

webkit.messageHandlers.callbackHandler.postMessage(data);

The native app catches the message in a delegate called:

本机应用程序在名为的委托中捕获消息:

(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message

and perform the corresponding actions

并执行相应的操作

here is the relevant code:

这是相关代码:

ajaxHandler.js -

//Every time an Ajax call is being invoked the listener will recognize it and  will call the native app with the request details

$( document ).ajaxSend(function( event, request, settings )  {
    callNativeApp (settings.data);
});

function callNativeApp (data) {
    try {
        webkit.messageHandlers.callbackHandler.postMessage(data);
    }
    catch(err) {
        console.log('The native context does not exist yet');
    }
}

My ViewController delegate are:

我的ViewController委托是:

@interface BrowserViewController : UIViewController <UIWebViewDelegate, WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler, UIWebViewDelegate>

And in my viewDidLoad(), I'm creating a WKWebView:

在我的viewDidLoad()中,我正在创建一个WKWebView:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc]init];
[self addUserScriptToUserContentController:configuration.userContentController];
appWebView = [[WKWebView alloc]initWithFrame:self.view.frame configuration:configuration];
appWebView.UIDelegate = self;
appWebView.navigationDelegate = self;
[appWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString: @"http://#############"]]];                                                     

Here is the addUserScriptToUserContentController:

这是addUserScriptToUserContentController:

- (void) addUserScriptToUserContentController:(WKUserContentController *) userContentController{
    NSString *jsHandler = [NSString stringWithContentsOfURL:[[NSBundle mainBundle]URLForResource:@"ajaxHandler" withExtension:@"js"] encoding:NSUTF8StringEncoding error:NULL];
    WKUserScript *ajaxHandler = [[WKUserScript alloc]initWithSource:jsHandler injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
    [userContentController addScriptMessageHandler:self name:@"callbackHandler"];
    [userContentController addUserScript:ajaxHandler];
}

更多相关文章

  1. 如何访问远程节点。浏览器中的js应用程序,而不是本地主机
  2. 如果外部应用程序更改了持久模型(服务器数据库),AngularJS可以自
  3. 在两台服务器上有效地在两个Django应用程序之间进行通信(多租户)
  4. 如何停止Py_Initialise应用程序的崩溃?
  5. 轮询Web服务的最佳方式(例如,对于Twitter应用程序)
  6. 乘客的Django应用程序显示空主页
  7. 用于在Google App Engine上构建应用程序的最佳开源IDE?
  8. 获取Android应用程序的Linux UID
  9. 在Linux中的脚本中安装应用程序

随机推荐

  1. Android开发中 ListView 不能不说的几个
  2. TextView 控件使用
  3. Android――全屏显示的两种方式
  4. Android studio中相对布局组件排列方式
  5. Activity的四种启动模式和onNewIntent()
  6. Android中设置动画循环旋转的方法
  7. Android 中文API (33) ―― Checkable
  8. EditText高度的改变
  9. Mono For Android 之 配置环境
  10. Android NDK 学习之传递类对象