Ali Hassani
Ali Hassani

Reputation: 43

UIWebView retrieve a link and navigate to it

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?

Upvotes: 2

Views: 277

Answers (2)

Cezar
Cezar

Reputation: 56332

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.

Upvotes: 1

Sergey Kuryanov
Sergey Kuryanov

Reputation: 6114

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"];

Upvotes: 0

Related Questions