swift - 如何在 swift 4.0 中读取 UIWebView 的响应或输出?stringByEvaluatingJavaScript(from:) 有多少种不同的方法
问题描述
我在 UIWbView 中打开一个 URL,用户可以在其中输入 OTP,提交后将提供以下响应:
{"success":true,""message":支付成功"}
我希望如果success的值为true,我将从 superview 中删除 UIWebView 。
我不知道如何读取 UIWebView 的响应。
解决方案
我尝试了字符串的数量作为参数,stringByEvaluatingJavaScript(from:)
但最后我通过传递"document.body.innerHTML"
.
"document.getElementById('someElement').innerText"
如果要从 HTML 元素/组件(如文本字段等)读取数据,可以使用,。
func webViewDidFinishLoad(_ webView: UIWebView) {
print("WebView did finish load...")
if !webView.isLoading {
Loader.hideLoading()
}
let str = webView.stringByEvaluatingJavaScript(from: "document.body.innerHTML")
if (str?.contains("\"success\":true"))! {
print("Success is true")
self.viewForWebView.removeFromSuperview()
}else {
print("Success is false or not found")
}
}
推荐阅读
- c++ - 当 CMake 还依赖于其他现有的第三方库时,CMake 将多个静态库合并到一个库中
- javascript - javascript中复选框的奇怪行为
- flutter - 如何在脚手架块的 FutureBuilder 中实现 listview.builder?
- postgresql - pgadmin4 无法启动,如何解决?
- css - 这是 CSS 或 Angular 动画之间更好的方法,以支持浏览器和 NativeScript 移动动画
- python - NameError:名称“TabularList”未定义
- c# - 在 Asp.Net Core 中调用 Task.Yield 是否确保继续在 ThreadPool 上运行?
- java - 在 Java 中使用 SQL 数据库
- javascript - Discord.js v13 在一定时间后删除消息
- python - Pandas:基于预定义类别的字符串列创建二进制列(虚拟/单热编码表)