Cristian
Cristian

Reputation: 7145

Programmatically Open Link in WebView

I have an NS Window with a WebView.

My program takes in a search query and executes a Google search with it, the results being displayed in the WebView, like a browser.

Instead of displaying the search results in the WebView, I'd like to automatically open the first link and display the contents of that result instead.

As a better example, how do I display the contents of the first result of Google in a WebView?

Is this even possible?

Any help greatly appreciated. Thanks!

Upvotes: 0

Views: 663

Answers (2)

calimarkus
calimarkus

Reputation: 9977

You could use the direct Google Search API. That would be more convinient.
https://developers.google.com/custom-search/v1/cse/list?hl=de-DE

Also you could also try to make a google request like the "I'm feeling lucky" button, which will direct you automatically to the first search result.

If you have to parse the HTML, you need to have a look at the HTML structure of the google result page. Look for specific id and class css properties in the div and a tags. If you found the ones, where the actual results are you can start parsing that content. Also i guess it would be easier to put some javascript together, that will find the first result and open it. (More easier than parsing the HTML using obj-c). You can evaluate javascript in the webview using [myWebView stringByEvaluatingJavaScriptFromString: @"put your js code here"].

Upvotes: 3

shkschneider
shkschneider

Reputation: 18243

Sure it is possible.

The first way to accomplish that that goes through my head is to parse the HTML response from Google, then launch a WebView with the first link you extracted.

Take a look at regular expressions to make it easy.

Upvotes: 0

Related Questions