Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-06-2012, 01:39 AM   #1 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 47
at0m87 is on a distinguished road
Question UIWebView - how to extract the HTML source code from webpages like Facebook??

Hi guys, i have an example of a source code which programatically sets the value of fields in an HTML.

The part of the source code is as such:

Code:
- (void)viewDidLoad
{
    NSLog(@"viewDidLoad");
    [super viewDidLoad];

    myWebView.delegate = self;
 
    //---formulate the HTML string---
    NSString *str = [[[NSString alloc] initWithString:
                    @"<html>"
                     "    <body>"
                     "        <h1>The fields below are filled in programmatically</h1>"
                     "        <input id='username' value='UserName' />"
                     "        <input id='password' value='Password' type='password' />"
                     "    </body>"
                     "</html>"
                     ] autorelease];
    
    //---load the UIWebView with the html string---
    [myWebView loadHTMLString:str baseURL:nil];
}

//---wait for the UIWebView to finish loading---
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSLog(@"webViewDidFinishLoad");
    //---access the 2 fields in the HTML---
    [myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('username').value='Peter';"];
    [myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('password').value='TopSecret';"];
}
I would like to programmatically set value of fields in an actually webpage like Facebook instead of a custom HTML like such written above.

How should i replace the "str" to an actual webpage's html source code so that i can pick out the "ElementbyId"?
at0m87 is offline   Reply With Quote
Old 02-07-2012, 01:33 AM   #2 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 47
at0m87 is on a distinguished road
Default

Bump! anyone could help with my problems or could point me to any tutorials or guides?
at0m87 is offline   Reply With Quote
Old 02-07-2012, 02:22 AM   #3 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Use [UIWebView loadRequest] to connect to the URL you want.
fiftysixty is offline   Reply With Quote
Old 02-07-2012, 02:39 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 47
at0m87 is on a distinguished road
Default

Quote:
Originally Posted by fiftysixty View Post
Use [UIWebView loadRequest] to connect to the URL you want.
Hi thank you for your reply, i can load a URL no problem.

Code:
 NSURL *url = [NSURL URLWithString:loginObj.loginURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [myWebView loadRequest:request];
but after loading the URL, how do i retrieve the HTML of the website to use this code?

Code:
[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('username').value='peter';"];
at0m87 is offline   Reply With Quote
Old 02-07-2012, 02:50 AM   #5 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Once the loadRequest has finished, the UIWebView contains the HTML already. Any calls you make to stringByEvaluationJavaScript... work on that HTML.
fiftysixty is offline   Reply With Quote
Old 02-07-2012, 04:22 AM   #6 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 47
at0m87 is on a distinguished road
Default

Quote:
Originally Posted by fiftysixty View Post
Once the loadRequest has finished, the UIWebView contains the HTML already. Any calls you make to stringByEvaluationJavaScript... work on that HTML.
Okay i did as per your suggestion and wrote this:

Code:
- (void)viewDidLoad
{
    NSLog(@"webView DidLoad");
    [super viewDidLoad];
    [self showWebView]; 
    
}


-(void)showWebView{
    appDelegate = (LoginDBAppDelegate *)[[UIApplication sharedApplication] delegate];
    
    loginObj = [appDelegate.loginArray objectAtIndex:currentSelectedRow];
    NSLog(@"%@", loginObj.loginURL);
    
    NSURL *url = [NSURL URLWithString:loginObj.loginURL];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [myWebView loadRequest:request];
    
    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) target:self selector:@selector(spin) userInfo:nil repeats:YES];
    
}

//--activity indicator to show the loading of webpages
- (void)spin{
    if (!myWebView.loading){
        [activity stopAnimating];
        //[self webViewDidFinishLoad];
        NSLog(@"Stop Spinning");
        
    }
    else{
        [activity startAnimating];
        NSLog(@"Spinning");

    }
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    //---access the 2 fields in the HTML---
    NSLog(@"webView DidFinishLoad");
    [myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('username').value='peter';"];
    [myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('password').value='password';"];
}
i run the program, the HTML loaded (which in this case was Facebook) but this method

Code:
- (void)webViewDidFinishLoad:(UIWebView *)webView
wasn't called so the credentials were not filled in.

In the sample code i had shown in my first post, the above mentioned method was executed and the credentials filled in. Now it just wasn't called at all.

Please let me know where i had done wrong in my code thanks =)

Last edited by at0m87; 02-07-2012 at 04:53 AM.
at0m87 is offline   Reply With Quote
Old 02-07-2012, 04:34 AM   #7 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Looks like you forgot to set the UIWebView delegate in your new code.
fiftysixty is offline   Reply With Quote
Old 02-07-2012, 04:37 AM   #8 (permalink)
Registered Member
 
Join Date: Nov 2009
Location: Helsinki
Posts: 304
fiftysixty is on a distinguished road
Default

Also, I hope those are not your real Facebook username and password in the code you just posted...
fiftysixty is offline   Reply With Quote
Old 02-07-2012, 04:54 AM   #9 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 47
at0m87 is on a distinguished road
Default

Quote:
Originally Posted by fiftysixty View Post
Also, I hope those are not your real Facebook username and password in the code you just posted...
oops. thanks for pointing it out..
at0m87 is offline   Reply With Quote
Old 02-07-2012, 05:05 AM   #10 (permalink)
Registered Member
 
Join Date: Jan 2012
Posts: 47
at0m87 is on a distinguished road
Default

Quote:
Originally Posted by fiftysixty View Post
Looks like you forgot to set the UIWebView delegate in your new code.
Do you mean setting this?

Code:
myWebView.delegate = self;
i added it in now, and yes, the method is being called. may i ask, what is the significant of setting the UIWebView delegate, why do we need to set it?

Now tat the method in question is executed, but the credentials aren't filling up. i suppose could be because the "id" were incorrect?

Update*
i tried playing around with different id and found that the email field's id is "email", instead of "Email"
Is there a way i can say for sure find out what each field's ID is?

update**
I figured out how to find out for sure what is the ID used for different field. i went to facebook's login page and click on view source code and i got it for there. Success as i was able to auto fill in all the credentials and successfully login in to Facebook from my App =))

Thanks fiftysixty!

Last edited by at0m87; 02-07-2012 at 05:53 AM.
at0m87 is offline   Reply With Quote
Reply

Bookmarks

Tags
html, javascript, uiwebview

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 404
17 members and 387 guests
Apptronics RBC, Atatator, chiataytuday, dre, FrankWeller, gwelmarten, ipodphone, jeroenkeij, jleannex55, LunarMoon, MAMN84, n00b, pbart, reficul, Retouchable, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,676
Threads: 94,124
Posts: 402,909
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jleannex55
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 06:15 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0