ios - WKWebView 检查 HTML 表单提交

标签 ios objective-c swift swift4 wkwebview

我的任务是将一个 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/

相关文章:

ios - 自定义 UIFont 基线偏移

ios - TableViewCell 中的 Swift 3 UISwitch 在滚动时丢失状态

ios - 使用 UITableView 中的核心图形将图像过滤器应用于图像

IOS App登录后切换 View

objective-c - 使用 Singleton 类的全局变量 NSMuteableArray

ios - 显示 YouTube 注释 - iOS 应用程序

objective-c - 在 iPhone 应用程序中执行请求时显示加载消息的最佳用途是什么

iOS:动画在 observeValueForKeyPath 中不起作用

ios - 在 UIImageView 中剪裁的圆角

ios - UICollectionViewCell Segue 到新的 ViewController