UIWebView 是否可以保存和自动填充以前输入的表单值(例如,用户名和密码)? [英] Is it possible for a UIWebView to save and autofill previously entered form values (e.g., username & password)?

查看:36
本文介绍了UIWebView 是否可以保存和自动填充以前输入的表单值(例如,用户名和密码)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个 iPhone 应用程序,它只是具有基于表单的登录的现有移动网站的 UIWebView.当我在 iPhone Safari 上登录移动网站时,系统会提示我保存我的用户名/密码,然后在我稍后返回该网站时自动填充.

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.

我想在 UIWebView 中启用相同的功能,但在我的一生中,我不知道如何去做.有什么想法吗?

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?

按照迈克尔的基本模型(参见接受的答案),我能够完成这项工作.这是我所做的:

Following Michael's basic model (see accepted answer), I was able to get this done. Here's what I did:

设置数据

- (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 钥匙串.

Note that I'm using Buzz Andersen's awesome SFHFKeychainUtils package to store sensitive data to the iOs Keychain.

为了让 SFHFKeychainUtils 正常工作,你需要做一些事情:

In order to get SFHFKeychainUtils working, you need to do a few things:

  1. SFHFKeychainUtils.hSFHFKeychainUtils.m 添加到您的项目
  2. Security.framework 添加到您的项目
  3. #import #import SFHFKeychainUtils.h"
  1. Add SFHFKeychainUtils.h and SFHFKeychainUtils.m to your project
  2. Add the Security.framework to your project
  3. #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:

  • 创建您的 uiweb 视图
  • 创建一个 nsurlrequest
  • 在您的 webview 委托页面加载函数触发后,查看请求的 http 正文
  • 找到登录表单(常见登录表单的正则表达式?)
  • 检索让用户可以选择保存它,然后再检索

这篇关于UIWebView 是否可以保存和自动填充以前输入的表单值(例如,用户名和密码)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆