简体   繁体   English

从Javascript在iPhone上加载自定义URL方案?

[英]Loading a custom URL scheme on iPhone from Javascript?

I have an application that uses stringByEvaluatingJavaScriptFromString to modify HTML that I have no access to (like Greasemonkey). 我有一个使用stringByEvaluatingJavaScriptFromString修改无法访问的HTML的应用程序(例如Greasemonkey)。 Specifically, I want to store the username and password from a login form in NSUserDefaults when the onSubmit event is fired from JS. 具体来说,当从JS触发onSubmit事件时,我想将登录表单中的用户名和密码存储在NSUserDefaults

I already have a custom URL scheme and an onSubmit handler. 我已经有一个自定义URL方案和一个onSubmit处理程序。 I can pass the URL a username and password, and it will store it. 我可以向URL传递用户名和密码,并将其存储起来。 I am only having issues with the onSubmit handler: how can I save data with my custom URL before submitting the form? 我的onSubmit处理程序只有问题:提交表单之前,如何使用自定义URL保存数据?

I am not using any Javascript frameworks, and my code only targets Mobile Safari. 没有使用任何Javascript框架,我的代码仅针对Mobile Safari。

Apologies for not directly answering your question, but I think you may want to consider an easier option. 很抱歉没有直接回答您的问题,但我认为您可能想考虑一个更简单的选择。 You should be able to sniff a form submission without injecting any javascript into the UIWebView . 您应该能够嗅探表单提交而无需将任何JavaScript注入UIWebView Just implement something like this in your UIWebViewDelegate : 只需在您的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;
}

I've left the implementations of parseUsernameFromRequest: , parsePasswordFromRequest: , and saveUsername:andPassword: to your imagination. 我将parseUsernameFromRequest:parsePasswordFromRequest:saveUsername:andPassword:的实现留给了您的想象。

(Hint: If the form uses the GET method, then you can get its parameters using [[request URL] parameterString] . If the form uses the POST method, then you can use [[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding] .) (提示:如果表单使用GET方法,则可以使用[[request URL] parameterString]获取其参数。如果表单使用POST方法,则可以使用[[NSString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding] 。)

By the way, you should never store sensitive information like passwords in NSUserDefaults. 顺便说一句,您永远不要在NSUserDefaults中存储敏感信息,例如密码。 Apple provides Keychain Services for storing this type of information securely. 苹果提供钥匙串服务,用于安全地存储此类信息。 Unfortunately working with the Keychain Services is surprisingly complex, so you may want to check out Buzz Andersen's Simple iPhone Keychain Code . 不幸的是,使用Keychain Services的过程非常复杂,因此您可能需要查看Buzz Andersen的Simple iPhone Keychain Code

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM