1

I have an application that uses stringByEvaluatingJavaScriptFromString to modify HTML that I have no access to (like Greasemonkey). Specifically, I want to store the username and password from a login form in NSUserDefaults when the onSubmit event is fired from JS.

I already have a custom URL scheme and an onSubmit handler. I can pass the URL a username and password, and it will store it. I am only having issues with the onSubmit handler: how can I save data with my custom URL before submitting the form?

I am not using any Javascript frameworks, and my code only targets Mobile Safari.

4

1 回答 1

1

很抱歉没有直接回答您的问题,但我认为您可能想考虑一个更简单的选择。您应该能够嗅探表单提交,而无需将任何 javascript 注入UIWebView. 只需在您的UIWebViewDelegate

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    if ([[[request URL] absoluteString] hasPrefix:@"https://www.thirdpartysite.com/theirLoginScript"]) {
        NSString *username = [self parseUsernameFromRequest:request];
        NSString *password = [self parsePasswordFromRequest:request];
        [self saveUsername:username andPassword:password];
    }
    return YES;
}

我已经将 、 和 的实现留给了parseUsernameFromRequest:parsePasswordFromRequest:saveUsername:andPassword:想象。

(提示:如果表单使用 GET 方法,则可以使用 获取其参数[[request URL] parameterString]。如果表单使用 POST 方法,则可以使用[[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding]。)

顺便说一句,你不应该在 NSUserDefaults 中存储敏感信息,比如密码。Apple 提供用于安全存储此类信息的钥匙串服务。不幸的是,使用 Keychain Services 非常复杂,因此您可能需要查看 Buzz Andersen 的Simple iPhone Keychain Code

于 2011-04-22T18:21:08.867 回答