Ios5 如何在UIWebView iOS应用程序中自动填写用户名和密码?
我正在通过在线查看各种来源的代码示例来自学如何编写iPhone应用程序,因此可以公平地说,我还不懂这门语言 我很抱歉,我已经成功地构建了一个UIWebView浏览器应用程序,可以进入登录页面。然而,我正试图通过拥有 按照拜伦关于堆栈溢出问题的代码,我试着追随他的脚步 但是,当以下代码行在应用程序中处于活动状态时,浏览器将仅加载空白页Ios5 如何在UIWebView iOS应用程序中自动填写用户名和密码?,ios5,uiwebview,autofill,Ios5,Uiwebview,Autofill,我正在通过在线查看各种来源的代码示例来自学如何编写iPhone应用程序,因此可以公平地说,我还不懂这门语言 我很抱歉,我已经成功地构建了一个UIWebView浏览器应用程序,可以进入登录页面。然而,我正试图通过拥有 按照拜伦关于堆栈溢出问题的代码,我试着追随他的脚步 但是,当以下代码行在应用程序中处于活动状态时,浏览器将仅加载空白页 -(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType; {
我真的很感谢任何帮助我回到正轨的帮助。多谢各位
下面是我的全部代码:
#import "ViewController.h"
// 6/13/2012 added to cache username and password
#import
#import "SFHFKeychainUtils.h"
// -----
@interface ViewController ()
@end
@implementation ViewController
@synthesize webView;
@synthesize spinner;
-(IBAction)goBack:(id)sender{
if ([webView canGoBack]) {
[webView goBack];
}
}
-(IBAction)goForward:(id)sender{
if ([webView canGoForward]){
[webView goForward];
}
}
- (void)viewDidLoad
{
NSURL *url = [NSURL URLWithString:@"http://xxxxxx.com/weblink/hh_login.html"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
// 6/13/2012 added to cache username and password
- (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)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[spinner startAnimating];
}
//- (void)webViewDidFinishLoad:(UIWebView *)webView {
// [spinner stopAnimating];
//}
// 6/13/2012 added to cache username and password
- (void)webViewDidFinishLoad:(UIWebView *)webView{
[spinner stopAnimating];
//verify view is on the login page of the site (simplified)
NSURL *requestURL = [self.webView.request URL];
if ([requestURL.host isEqualToString:@"http://xxxxxx.com/weblink/hh_login.html"]) {
//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];
}
}
}
// -----
@end
试试下面一个
加载webview后,您需要填写凭据。
下面是加载WebView时调用的UIWebView的委托方法。此时,在加载UIWebView之前传递的凭据不正确
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(!isLoggedIn){//just load only onetime.
//pass the login Credintails into textfield of WebView.
NSString* userId = @"userName" //here just replace that string to the username
NSString* password = @"password";//here just replace that string to the password
if(userId != nil && password != nil ){
NSString* jScriptString1 = [NSString stringWithFormat:@"document.getElementById('username').value='%@'", userId];
//username is the id for username field in Login form
NSString* jScriptString2 = [NSString stringWithFormat:@"document.getElementById('password').value='%@'", password];
//here password is the id for password field in Login Form
//Now Call The Javascript for entring these Credential in login Form
[webView stringByEvaluatingJavaScriptFromString:jScriptString1];
[webView stringByEvaluatingJavaScriptFromString:jScriptString2];
//Further if you want to submit login Form Automatically the you may use below line
[webView stringByEvaluatingJavaScriptFromString:@"document.forms['login_form'].submit();"];
// here 'login_form' is the id name of LoginForm
}
isLoggedIn=TRUE;
}
}
它真的会帮你的。试试下面的一个
加载webview后,您需要填写凭据。
下面是加载WebView时调用的UIWebView的委托方法。此时,在加载UIWebView之前传递的凭据不正确
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if(!isLoggedIn){//just load only onetime.
//pass the login Credintails into textfield of WebView.
NSString* userId = @"userName" //here just replace that string to the username
NSString* password = @"password";//here just replace that string to the password
if(userId != nil && password != nil ){
NSString* jScriptString1 = [NSString stringWithFormat:@"document.getElementById('username').value='%@'", userId];
//username is the id for username field in Login form
NSString* jScriptString2 = [NSString stringWithFormat:@"document.getElementById('password').value='%@'", password];
//here password is the id for password field in Login Form
//Now Call The Javascript for entring these Credential in login Form
[webView stringByEvaluatingJavaScriptFromString:jScriptString1];
[webView stringByEvaluatingJavaScriptFromString:jScriptString2];
//Further if you want to submit login Form Automatically the you may use below line
[webView stringByEvaluatingJavaScriptFromString:@"document.forms['login_form'].submit();"];
// here 'login_form' is the id name of LoginForm
}
isLoggedIn=TRUE;
}
}
这真的会帮到你。我只有一个问题。。。我创建isLoggedIn作为BOOL属性,如下所示:@property(非原子,赋值)BOOL isLoggedIn;。。。这会导致属性需要一个类似以下空间:_isLoggedIn。。。无论出于何种原因,除非我删除isLoggedIn,否则表单不会接受用户和pW。。。但我真的很想用它!帮助;)我只有一个问题。。。我创建isLoggedIn作为BOOL属性,如下所示:@property(非原子,赋值)BOOL isLoggedIn;。。。这会导致属性需要一个类似以下空间:_isLoggedIn。。。无论出于何种原因,除非我删除isLoggedIn,否则表单不会接受用户和pW。。。但我真的很想用它!帮助;)