简体   繁体   English

如何在 WKWebView 中访问 WKWebView 内容?

[英]How to access WKWebView content in WKWebView?

I need to check wether content of the WKWebView HTML content contains a certain string.我需要检查 WKWebView HTML 内容的内容是否包含某个字符串。 How can I do it?我该怎么做? I have not fount any content or data attribute on it.我没有找到任何内容或数据属性。

Method 1: Do it in Objective-C方法 1:在 Objective-C 中进行

NSString *str = [NSString stringWithContentsOfURL:wkwebview.URL usedEncoding: NSUTF8StringEncoding error:nil];
NSRange range = [str rangeOfString:@"string to find"];
if (range.location != NSNotFound) {
    //found
}

Method 2: Do it in JQuery方法 2:在 JQuery 中执行

NSString *js = @"var htmlString = $('body').html().toString();
var index = htmlString.indexOf('string to find');
if (index != -1) {alert('found')}";

[wkWebView evaluateJavaScript:js completionHandler:nil];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM