我的任务是将Objective-C应用程序转换为完全Swift 4.该应用程序基本上是一个包装我的公司网站的webbroswer。为了让用户每次都抱怨输入用户名/密码,应用程序需要检查提交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,因为我似乎无法为WKWebView找到任何类型的UIWebViewNavigationTypeFormSubmitted
。
有任何帮助吗?
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
//...
}
}
}
答案 0 :(得分:2)
应该是
if navigationType == UIWebViewNavigationType.formSubmitted
或
if navigationType == .formSubmitted