4

One of the functions of an app I am making involves logging the user into our Campus Portal website. For ease of use, the user may enter a username and password into the app once, and it will be saved for all future log-ins. When the user clicks a button, the information will automatically be sent to log in, and the website will be displayed in a UIWebView.

Let us say that the username and password are each stored in an NSString. How can I use this data to log in to this website programmatically and display the page it in a UIWebView?

I know little about posting and forms, so any help is appreciated.

Would something like this Stackoverflow answer help?

Here's the shell of my code for this

- (IBAction)btnGo:(id)sender {
    username = usernameField.text;
    password = passwordField.text;
    if (saveSwitch.isOn) {
        //Save data if the user wants
        AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        usernameSaved = username;
        passwordSaved = password;
        [appDelegate.listOfPortalCredentials replaceObjectAtIndex:0 withObject:usernameSaved];
        [appDelegate.listOfPortalCredentials replaceObjectAtIndex:1 withObject:passwordSaved];
    }
    //Insert username and password to web form, log in to portal
    //This is where I need help
}

Edit:

Thanks to Karthik I was able to find the HTTP Post using Firebug. It gave me:

appName=ridgefield&portalUrl=portal%2Fridgefield.jsp%3F%26rID%3D0.18423783694092&username=<username>&password=<password>&B1=Log+In&url=portal%2Fmain.xsl%3FrID%3D0.6845596700302482&lang=en

where and represent the real username and password. I believe this is what I need. Using this, how can I display the logged-in page in a UIWebView? Do I just need to load the above URL in the UIWebView?

Community
  • 1
  • 1
dgund
  • 3,459
  • 4
  • 39
  • 64
  • I would, but I have nothing that pertains to this. I am asking how to do so. I'll post some code associated with what I want to do though... – dgund Dec 13 '12 at 22:19
  • 1
    Do you have any control on the server? – Moxy Dec 13 '12 at 22:20
  • What solutions or approaches have you tried? No one's going to write your code for you. Try Google, try a few things first, then post. All the best. – Bryan Allo Dec 13 '12 at 22:20
  • It's a third party server. I do appreciate your suggestions, and I understand the rules to Stackoverflow. No one needs to write my code, but just a pointer on where to go? If I view the page source on the page the code for the form is visible. Is this all I need? – dgund Dec 13 '12 at 22:23
  • You could consider using something like Charles Proxy to capture the web traffic between your browser and the site to discover what it is you need to send and then build the appropriate NSURLRequest and load it in your web view. – Ben Flynn Dec 14 '12 at 07:04
  • Perhaps something like http://stackoverflow.com/questions/9711246/ios-objective-c-uiwebview-autofill-and-execute#_=_ – dgund Dec 14 '12 at 13:55

1 Answers1

3

Use UIWebView, and do a http POST to https://ic.ridgefield.org/campus/verify.jsp with username and password.

To understand how it works, install Firebug on Firefox browser and go to 'Net' tab on firebug, and then open your website and enter some username/password.

You should mimic that action using code. (I always get invalid username or password response from server, coz i dont have an account and i try some random ones, and since there is no signup on the site, there is no way for me to verify this)

UIWebView* webView = [[UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];

NSString* username = @"";
NSString* password = @"";

NSURL* url = [NSURL URLWithString:@"https://ic.ridgefield.org/campus/verify.jsp"];

NSString* body = [NSString stringWithFormat:@"appName=ridgefield&username=%@&password=%@", username, password];
NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
request.HTTPMethod = @"POST";
request.HTTPBody = [body dataUsingEncoding:NSStringEncodingConversionAllowLossy];

[webView loadRequest:request];
Karthik
  • 1,386
  • 12
  • 7
  • Thank you a lot I used Firebug to get the HTTP Post. (See my edited question) Could you help me with how to execute it myself in a UIWebView? – dgund Dec 16 '12 at 16:18
  • Thank you so much. I will award you the bounty in two hours (Stackoverflow won't allow it until then) – dgund Dec 16 '12 at 20:07
  • thanks for this. I rework this in swift but exact same and its working. I wonder what if the website don't have login action? what if the website handling the login via javascript? – CaffeineShots Aug 07 '15 at 13:02
  • if it is js based, they should be using ajax calls, check the firebug net tab. – Karthik Dec 17 '15 at 01:36
  • Hi @KarthikSadhasivam am facing the similar issue now. I used firebug, when I tried in firebug I have seen "form_type=customer_login&utf8=%E2%9C%93&customer%5Bemail%5D=ragul%40test.in&customer%5Bpassword%5D =qwerty123" as post Source. and I have just modified your above code just changed the urls but always it shows "Invalid credentials" in app. Kindly guide me to resolve this error.. – Ragul Feb 18 '16 at 09:37