UIWebView 是否可以保存并自动填充先前输入的表单值(例如,用户名和密码)?

发布于 2024-10-13 11:17:52 字数 2733 浏览 3 评论 0原文

我正在构建一个 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 正常工作,您需要执行以下操作:

  1. SFHFKeychainUtils.hSFHFKeychainUtils.m 添加到您的项目中
  2. 添加 Security.framework 到您的项目
  3. #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:

  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"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼性发作 2024-10-20 11:17:52

从我看来,我认为没有一个简单的方法可以做到这一点。以下是可能的工作原理:

  • 创建 uiwebview
  • 在 webview 委托页面加载函数触发后创建 nsurlrequest
  • 在请求的 http 正文中查找
  • 登录表单(常见登录表单的正则表达式?)
  • 检索为用户提供保存并稍后检索的选项

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文