问题描述
我正在构建一个iPhone应用程序,它只是一个现有移动网站的基于表单登录的 UIWebView
。当我在iPhone Safari上登录移动网站时,系统会提示我保存用户名/密码,然后在我稍后返回网站时自动填充。
我想在 UIWebView
中启用相同的功能,但在我的生活中,我可以不知道该怎么做。任何想法?
解决方案
看到接受的答案),我能够完成这件事。以下是我所做的:
设置数据
- (BOOL) webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)请求导航类型:(UIWebViewNavigationType)navigationType; {
//保存表单数据
if(navigationType == UIWebViewNavigationTypeFormSubmitted){
//从页面获取数据
NSString * username = [self.webView stringByEvaluatingJavaScriptFromString:@document.myForm.username.value];
NSString * password = [self.webView stringByEvaluatingJavaScriptFromString:@document.myForm.password.value];
//在本地存储值
[[NSUserDefaults standardUserDefaults] setObject:username forKey:@username];
[SFHFKeychainUtils storeUsername:username andPassword:password forServiceName:@MyServiceupdateExisting:YES error:nil];
$ b 获取数据
$> / b>
- (void)webViewDidFinishLoad:(UIWebView *)webView {
$ b $ // //验证视图位于登录页面(简体)
NSURL * requestURL = [self.webView.request URL];
if([requestURL.host isEqualToString:@www.mydomain.com]){
//检查存储的登录凭证
NSString * username = [[NSUserDefaults standardUserDefaults ] objectForKey:@username];
if(username.length!= 0){
//创建js字符串
NSString * loadUsernameJS = [NSString stringWithFormat:@document.myForm.username .value ='%@',用户名];
NSString * password = [SFHFKeychainUtils getPasswordForUsername:username andServiceName:@MyServiceerror:nil];
if(password.length == 0)password = @;
NSString * loadPasswordJS = [NSString stringWithFormat:@document.myForm.password.value ='%@',password];
//自动填写表单
[self.webView stringByEvaluatingJavaScriptFromString:loadUsernameJS];
[self.webView stringByEvaluatingJavaScriptFromString:loadPasswordJS];
$ / code>
请注意,我使用Buzz Andersen的令人敬畏的包来存储敏感数据为了使SFHFKeychainUtils正常工作,您需要做一些事情:
- 在您的项目中添加
SFHFKeychainUtils.h
和 SFHFKeychainUtils.m
- 将
Security.framework
添加到您的项目中
-
#import< Security / Security。 h>和 #importSFHFKeychainUtils.h
解决方案从我看,我不认为有一个简单的方法来做到这一点。以下是可能工作原理:
- 创建您的uiwebview
- 在您的webview委托页面加载函数触发查找请求的http正文后,创建一个nsurlrequest
-
- 找到登录表单(正则表达式常见的登录表单)
- 检索让用户选择保存并稍后检索
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
and SFHFKeychainUtils.m
to your project - Add the
Security.framework
to your project #import <Security/Security.h>
and #import "SFHFKeychainUtils.h"
解决方案 From my looking I don't think there is an easy way to do it. Here is an idea of what might work though:
- create your uiwebview
- create a nsurlrequest
- after your webview delegate page loaded function fires look in the request's http body
- find the form for login (regex for common login forms?)
- retrieve give the user the option to save it and then retrieve later
这篇关于UIWebView是否可以保存并自动填写以前输入的表单值(例如,用户名和密码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!