我的任务是将一个 Objective-C 应用程序转换为完全 Swift 4。该应用程序基本上是一个包装我公司网站的网络浏览器。为了让用户提示每次都输入用户名/密码,应用程序需要检查提交 HTML 表单的时间并抓取网站并存储 u/p。
在 ObjC 中有一个方法:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
这让我可以检查表单何时提交:
static NSString * const FORM_USER = @"document.getElementById('sUserID').value";
static NSString * const FORM_PASS = @"document.getElementById('sPassword').value";
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
//save form data
if(navigationType == UIWebViewNavigationTypeFormSubmitted) {
dispatch_async(dispatch_get_main_queue(), ^{
//grab the data from the page
NSString *username = [self.webView stringByEvaluatingJavaScriptFromString: FORM_USER];
NSString *password = [self.webView stringByEvaluatingJavaScriptFromString: FORM_PASS];
//store values locally in the background
//.....
});
}
return true;
}
但是我在转换 Swift 4.1 时遇到了问题,因为我似乎找不到任何类型的 UIWebViewNavigationTypeFormSubmitted
用于 WKWebView。
有什么帮助吗?
let FORM_USER = "document.getElementById('sUserID').value"
let FORM_PASS = "document.getElementById('sPassword').value"
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation) {
if(webView.url == API_LOGIN_URL){
if(WHAT A DO I CHECK FOR here?){
//Grab IN LOGIN DETAILS
var name = webView.evaluateJavaScript("\(FORM_USER)", completionHandler: nil)
var pass = webView.evaluateJavaScript("\(FORM_PASS)", completionHandler: nil)
//store values locally
//...
}
}
}
最佳答案
应该是
if navigationType == UIWebViewNavigationType.formSubmitted
或
if navigationType == .formSubmitted
关于ios - WKWebView 检查 HTML 表单提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50822813/