2

I am adding a UIWebView to my app that should load a password protected webpage. It should then select a link from that page automatically and navigate to that page. This website changes it's links continuously so there is no way to select the URL of the intended page. I need to log in first and then select a link from the main page.

How can I write the code to search my main page after log in for the desired link and navigate to it?

Cezar
  • 55,636
  • 19
  • 86
  • 87
Ali Hassani
  • 43
  • 1
  • 6

2 Answers2

1

You can use Javascript to retrieve the link by its id and then load it:

[yourWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('yourLinkID').click();"];

To find what is the id for the link check its html tag on the page for the value of the id property.

Cezar
  • 55,636
  • 19
  • 86
  • 87
0

I think Regular Expressions will help.

//NSError will handle errors
NSError *error;
//Create URL for you page. http://example.com/index.php just an example
NSURL *pageURL = [NSURL URLWithString:@"http://example.com/index.php"];
//Retrive page code to parse it using regex
NSString *pageHtml = [NSString stringWithContentsOfURL:pageURL           
                                              encoding:NSUTF8StringEncoding 
                                                 error:&error];
if (error)
{
    NSLog(@"Error during retrieving page HTML: %@", error);
    //Will terminate your app
    abort();
    //TODO: handle connection error here
}
error = nil;
//Creating regex to parsing page html
//Information about regex patters you can easily find.
NSRegularExpression *regex = [[NSRegularExpression alloc] initWithPattern:@"<a[^>]*href=\"([^\"]*)\"[^>]*>mylink</a>"
                                                                  options:NSRegularExpressionCaseInsensitive
                                                                    error:&error];
if (error)
{
    NSLog(@"Error during creating regex: %@", error);
    //Will terminate your app
    abort();
    //TODO: handle regex error here
}
//Retrieving first match of our regex to extract first group
NSTextCheckingResult *match = [regex firstMatchInString:pageHtml
                                                options:0
                                                  range:NSMakeRange(0, [pageHtml length])];
NSString *pageUrl = [pageHtml substringWithRange:[match rangeAtIndex:1]];
NSLog(@"Page URL = %@", pageUrl);
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:pageUrl]]];

If your UIWebView already downloaded page with HTML you can replace

NSURL *pageURL = [NSURL URLWithString:@"http://example.com/index.php"];
NSString *pageHtml = [NSString stringWithContentsOfURL:pageURL encoding:NSUTF8StringEncoding error:&error];

with this:

NSString *pageHtml = [webview stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];
Sergey Kuryanov
  • 6,114
  • 30
  • 52
  • thanks so much for the quick reply. i should have explained that I'm quite new to programming. i searched information about regular expressions. would i be able to use this to for example find a link called mylink on a page and navigate to the continuously changing url for that link? thank you once again – Ali Hassani Mar 21 '13 at 01:32
  • the link name always remains the same but the url for that link is constantly changing. – Ali Hassani Mar 21 '13 at 01:33
  • Check edited answer. If this code will not work please post your site url to check. – Sergey Kuryanov Mar 21 '13 at 01:59
  • thank you so much for your post. this was exactly what I was looking for and a great help! I can't tell you how much I appreciate it. Best regards, Ali – Ali Hassani Mar 24 '13 at 19:18
  • hi Sergey, was wondering if you can help me with a related manner. I have included the regex in my WebViewDidFinishLoad method and everything works fine except after the target webpage is navigated to, there seems to be an infinite loop and the page goes blank. When I do an NSLog it seems it just keeps repeating the WebPageDidFinishLoad method infinitely. Am I doing something wrong here? Thanks – Ali Hassani Mar 28 '13 at 04:49
  • Maybe you can add some flag to indicate that you already downloaded target page. `if (!targetPageDownloaded) {//regex targetPageDownloaded = YES}` – Sergey Kuryanov Mar 28 '13 at 08:08