2

I would like to load a web page (containing a form: username and password) in a UIWebView, but I would like the web page to be filled when the UIWebView is loaded. I read so much stuff and tried so many things, but nothing works.

here is my code :

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://login.live.com"]];

// Specify that it will be a POST request
request.HTTPMethod = @"POST";

// This is how we set header fields
[request setValue:@"application/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"username" forHTTPHeaderField:@"session[email]"];

// Convert your data and set your request's HTTPBody property
NSString *stringData = @"some data";
NSData *requestBodyData = [stringData dataUsingEncoding:NSUTF8StringEncoding];
request.HTTPBody = requestBodyData;

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[self.webView loadRequest:request];
  • 1
    you need to use some javascript to fill form. – Shubhank Mar 19 '14 at 18:21
  • do you want to show that page on screen(UIWebView) after the page's HTML content loads? I mean do you want to first load HTML content, then load it onto webView? – santhu Mar 19 '14 at 18:23
  • after WebViewDid Finish loading.. parse the Html and fill the respected fields you want to fill – faiziii Mar 19 '14 at 18:52

2 Answers2

3

Let's say you have a form field defined like this:

<input type="text" id="mytext">

Then you can assign a value to this field as follows:

NSString *javascript = @"document.getElementById('mytext').value = 'new value'";
[webView stringByEvaluatingJavaScriptFromString:javascript];

The above code needs to run after your page has fully loaded. For example, you can make your view controller implement UIWebViewDelegate, declare the method - (void)webViewDidFinishLoad:(UIWebView *)webView and do it there.

TotoroTotoro
  • 17,524
  • 4
  • 45
  • 76
  • I also have to click on a button in the web page via the objective c, can i use the same kind of process? – user3439023 Mar 19 '14 at 19:46
  • Yes, you can use the code above as an example on how to run any JavaScript from within your app. Obviously you set fill multiple form values :) – TotoroTotoro Mar 19 '14 at 20:34
-2

EDITED: 20/03/2014 -: I added how to get the code from the UIWebView

If you want know when the page did finish loading, you should implement UIWebViewDelegate in your UIViewController:

Then you can detect when the page finish loading by implementing this method:

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    UIApplication* app = [UIApplication sharedApplication];
    app.networkActivityIndicatorVisible = NO;
    [_activityIndicator stopAnimating];
    _activityIndicator.hidden = TRUE;
}

In you case, you want to modify the code when the page it's loaded, so in the method (void)webViewDidFinishLoad:(UIWebView *)webView you should do the next to get the code as a NSString:

NSURL *requestURL = [[yourWebView request] URL];
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestURL encoding:NSASCIIStringEncoding error:&error];

For more details take a look to this answer.

I hope this help you.

Community
  • 1
  • 1
IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
  • Thank you for your answer but my question is really about modifying the web page... – user3439023 Mar 19 '14 at 18:38
  • Yes, this answer refers to the second question: _...in a UIWebView. **But I would like the webpage to be filled when the UIWebView is loaded…** I read so much stuff..._. If i find something about the first question, will post you the answer. – IgniteCoders Mar 19 '14 at 18:44
  • 1
    This's useful to know when the page is ready to can modify the code, if you try to modify it before finish loading, then there's no html code to modify. – IgniteCoders Mar 20 '14 at 12:58
  • This has absolutely no relevance to this question and doesn't answer it in anyway, my -1 remains. – Popeye Mar 20 '14 at 14:02
  • Sorry then, I think i am not understanding the question dues to my low english level. – IgniteCoders Mar 20 '14 at 16:33