Previous 1 2 3 Next 33 Replies Latest reply: May 23, 2011 9:36 AM by sptrakesh
G_Morales Level 1 Level 1 (0 points)

I want to login at a page with my username and password. I want that my app connects to the php page in a UIWebView and login.


iPhone 4, iOS 4
  • sptrakesh Level 4 Level 4 (1,785 points)

    There is nothing much to do. Just load the URL to the PHP page in your UIWebView and you should be all done.

  • K T Level 7 Level 7 (23,700 points)

    iOS Dev Center

     

    Coding How-To's

         Data Management Coding How-To's

              File Management: How Do I ...

                   ... Display data from a website?

  • G_Morales Level 1 Level 1 (0 points)

    Yes, my code loads the url but when I put my id and my password the webview doesn´t login. It doesnt do anything only loads the page. Do you want to see my code sptrakesh?

  • sptrakesh Level 4 Level 4 (1,785 points)

    That is strange.  The issue is unlikely to be with UIWebView itself (unless of course the result page is sending some markup that the web view cannot render, flash for instance).  Do you have access to the server logs?  If yes, that would be the best option to debug this issue.  Another quick and easy test is to try and load the PHP page from Safari and see if you can login.  UIWebView for the most part should behave in an identical fashion.

  • G_Morales Level 1 Level 1 (0 points)

    In Safari the php page loads, I know it is really strange! Do you have a sample code for something like this? Or do you want to see my code? The only thing is that the PHP page is in spanish because im from Mexico

  • sptrakesh Level 4 Level 4 (1,785 points)

    Sure, post your code, so we can try and spot any potential issues.

  • G_Morales Level 1 Level 1 (0 points)

    Header (.h)

     

    @interface UP_ViewController : UIViewController {

       

        IBOutlet UITextField *nameInput;

        IBOutlet UITextField *passInput;

        IBOutlet UILabel *greeting;

        UIAlertView *alert;

        UIAlertView *alert2;

        NSMutableData *webData;

       

    }

     

    @property(nonatomic, retain) IBOutlet UITextField *nameInput;

    @property(nonatomic, retain) IBOutlet UITextField *passInput;

    @property(nonatomic, retain) IBOutlet UILabel *greeting;

    @property(nonatomic, retain) NSMutableData *webData;

    @property(nonatomic, retain) UIAlertView *alert;

    @property(nonatomic, retain) UIAlertView *alert2;

     

    -(IBAction)buttonClick: (id) sender;

     

    @end

  • G_Morales Level 1 Level 1 (0 points)

    Main (.m)

     

    @synthesize nameInput;

    @synthesize passInput;

    @synthesize greeting;

    @synthesize webData;

    @synthesize alert;

    @synthesize alert2;

     

    - (void)dealloc

    {

        [super dealloc];

    }

     

    -(IBAction)buttonClick:(id)sender

    {

     

        NSString* username = nameInput.text;

        NSString* pass = passInput.text;

     

        if([nameInput.text isEqualToString:@"" ]||[passInput.text isEqualToString:@""])

        {

            alert2 = [[UIAlertView alloc] initWithTitle:@"UP Network" message:@"Por Favor Ingrese sus Datos" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];

            [alert2 show];

            [alert2 release];

            [nameInput resignFirstResponder];

            [passInput resignFirstResponder];

            return;

        }

     

        NSString *post =

        [[NSString alloc] initWithFormat:@"Nombre de usuario=%@&Contraseña=%@",username,pass];

     

        NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

     

        NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

     

        NSURL *url = [NSURL URLWithString:@"Nombre de Usuario;Contraseña@moodle.up.edu.mx/moodle/login/index.php"];

        NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

        [theRequest setHTTPMethod:@"POST"]; 

        [theRequest setValue:postLength forHTTPHeaderField:@"Content-Length"]; 

        [theRequest setHTTPBody:postData];     

     

        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

     

        if( theConnection )

        {

            webData = [[NSMutableData data] retain];

        }

        else

        {

     

        }

     

        [nameInput resignFirstResponder];

        [passInput resignFirstResponder];

     

    }

     

    -(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

    {

        [webData setLength: 0];

    }

     

    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

    {

        [webData appendData:data];

    }

     

    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

    {

        alert2 = [[UIAlertView alloc] initWithTitle:@"UP Network" message:@"Datos Incorrectos" delegate:self cancelButtonTitle:@"Continue" otherButtonTitles:nil];

        [alert2 show];

        [alert2 release];

        nameInput.text = nil;

        passInput.text = nil;

     

        [connection release];

        [webData release];

    }

     

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection

    {

        NSString *loginStatus = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

        NSLog(loginStatus);

    greeting.text = loginStatus;

        [loginStatus release];

     

        [connection release];

        [webData release];

    }

  • G_Morales Level 1 Level 1 (0 points)

    in spanish "Nombre de usuario" = english "Username"

    in spanish "Contraseña" = english "Password"

     

    moodle.up.edu.mx/moodle/login/index.php   is the pagei want to log in

  • sptrakesh Level 4 Level 4 (1,785 points)

    Looking at the HTML page source, it does not look like the form handler expects the username and password parameter (or form field) names to be in Spanish.  Does it work, if you use the same names as specified in the form source?

  • G_Morales Level 1 Level 1 (0 points)

    No i use the names in Spanish that are specified in te page:

     

    Screen shot 2011-05-16 at 2.06.09 PM.png

  • sptrakesh Level 4 Level 4 (1,785 points)

    Those are just labels, that is not what the form handler expects.  View source on the page and inspect the form HTML element.  You will see the actual names of the form fields.  When you post to the form handler, that is what the handler expects.  The labels are meaningless to the form handler.

  • G_Morales Level 1 Level 1 (0 points)

    And how do i do that? How do I see the real names? I`m practically a noob hahaha sorry

    Is my syntaxis of the code is correct? I something wrong?

  • sptrakesh Level 4 Level 4 (1,785 points)

    Open the

    moodle.up.edu.mx/moodle/login/index.php URL in your browser and use the menu or context menu to view source of the page.  Look for <form action="index.php".  Under that you will see a couple of <input type="text" name="xxx".  The name is what the form handler expects as the parameter names in the post.

Previous 1 2 3 Next