1

我正在通过在线查看各种来源的代码示例来自学如何编写 iPhone 应用程序,因此可以公平地说,我还不懂这种语言。

我已经成功构建了一个进入登录页面的 UIWebView 浏览器应用程序。但是,我试图通过让

按照拜伦关于他自己的堆栈溢出问题的代码,我试图追随他的脚步。

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

但是,当应用程序中存在以下代码行时,浏览器只会加载一个空白页面。

    -(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

4

1 回答 1

5

尝试以下一个

您需要在 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;

}
}

它真的会帮助你。

于 2012-10-12T09:44:49.017 回答