UIWebView 是否可以保存并自动填充先前输入的表单值(例如,用户名和密码)?
我正在构建一个 iPhone 应用程序,它只是具有基于表单的登录功能的现有移动网站的 UIWebView
。当我在 iPhone Safari 上登录移动网站时,系统会提示我保存用户名/密码,当我稍后返回该网站时,系统会自动填充该用户名/密码。
我想在 UIWebView
中启用相同的功能,但我一生都不知道该怎么做。有什么想法吗?
解决方案
按照迈克尔的基本模型(参见接受的答案),我能够完成这项工作。这就是我所做的:
设置数据
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
//save form data
if(navigationType == UIWebViewNavigationTypeFormSubmitted) {
//grab the data from the page
NSString *username = [self.webView stringByEvaluatingJavaScriptFromString: @"document.myForm.username.value"];
NSString *password = [self.webView stringByEvaluatingJavaScriptFromString: @"document.myForm.password.value"];
//store values locally
[[NSUserDefaults standardUserDefaults] setObject:username forKey:@"username"];
[SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:@"MyService" updateExisting:YES error:nil];
}
}
获取数据
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//verify view is on the login page of the site (simplified)
NSURL *requestURL = [self.webView.request URL];
if ([requestURL.host isEqualToString:@"www.mydomain.com"]) {
//check for stored login credentials
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
if (username.length != 0 ) {
//create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:@"document.myForm.username.value ='%@'", username];
NSString *password = [SFHFKeychainUtils getPasswordForUsername: username andServiceName:@"MyService" error:nil];
if (password.length == 0 ) password = @"";
NSString *loadPasswordJS = [NSString stringWithFormat:@"document.myForm.password.value ='%@'", password];
//autofill the form
[self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
[self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}
}
请注意,我正在使用 Buzz Andersen 的很棒的 SFHFKeychainUtils 包将敏感数据存储到 iOs 钥匙串。
为了使 SFHFKeychainUtils 正常工作,您需要执行以下操作:
- 将
SFHFKeychainUtils.h
和SFHFKeychainUtils.m
添加到您的项目中 - 添加
Security.framework 到您的项目
#import
和#import "SFHFKeychainUtils.h"
I'm building an iPhone app that is just a UIWebView
of an existing mobile site that has a form-based login. When I login to the mobile site on iPhone Safari, I'm prompted to save my username/password, and it's then autofilled when I go back to the site later.
I'd like to enable the same functionality in the UIWebView
, but for the life of me, I can't figure out how to do it. Any ideas?
Solution
Following Michael's basic model (see accepted answer), I was able to get this done. Here's what I did:
SETTING DATA
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
//save form data
if(navigationType == UIWebViewNavigationTypeFormSubmitted) {
//grab the data from the page
NSString *username = [self.webView stringByEvaluatingJavaScriptFromString: @"document.myForm.username.value"];
NSString *password = [self.webView stringByEvaluatingJavaScriptFromString: @"document.myForm.password.value"];
//store values locally
[[NSUserDefaults standardUserDefaults] setObject:username forKey:@"username"];
[SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:@"MyService" updateExisting:YES error:nil];
}
}
GETTING DATA
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//verify view is on the login page of the site (simplified)
NSURL *requestURL = [self.webView.request URL];
if ([requestURL.host isEqualToString:@"www.mydomain.com"]) {
//check for stored login credentials
NSString *username = [[NSUserDefaults standardUserDefaults] objectForKey:@"username"];
if (username.length != 0 ) {
//create js strings
NSString *loadUsernameJS = [NSString stringWithFormat:@"document.myForm.username.value ='%@'", username];
NSString *password = [SFHFKeychainUtils getPasswordForUsername: username andServiceName:@"MyService" error:nil];
if (password.length == 0 ) password = @"";
NSString *loadPasswordJS = [NSString stringWithFormat:@"document.myForm.password.value ='%@'", password];
//autofill the form
[self.webView stringByEvaluatingJavaScriptFromString: loadUsernameJS];
[self.webView stringByEvaluatingJavaScriptFromString: loadPasswordJS];
}
}
}
Note that I'm using Buzz Andersen's awesome SFHFKeychainUtils package to store sensitive data to the iOs Keychain.
In order to get SFHFKeychainUtils working, you need to do a few things:
- Add
SFHFKeychainUtils.h
andSFHFKeychainUtils.m
to your project - Add the
Security.framework
to your project #import <Security/Security.h>
and#import "SFHFKeychainUtils.h"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从我看来,我认为没有一个简单的方法可以做到这一点。以下是可能的工作原理:
From my looking I don't think there is an easy way to do it. Here is an idea of what might work though: