Sibin
Sibin

Reputation: 133

How to a invoke google search on a subview with a search string passed from Main view on an iPhone app

My application has two views and I want to invoke a google search on Second view with a search string (it is name of a product. For example 'Samsung Mobile') passed from application's Main view.

I dont want to enter product name in Google search field manually. It should be done automatically when I press the button on the Main View and the result page should be displayed on Sub View.

-(void) setLabelText:(NSString *) myNewText
{
    [productName setText:myNewText];

    NSURL *theURL =[NSURL URLWithString:@"http://www.google.com"];
    NSURLRequest *theRequest = [NSURLRequest requestWithURL:theURL];

    [webSearchView loadRequest:theRequest];
}

Just wondering is it possible to pass the search string as a parameter with the above function.

Upvotes: 2

Views: 248

Answers (2)

user2067021
user2067021

Reputation: 4539

As per H2CO3's answer, but I think the Google search URL may have changed. I also prefer https over http:

NSString *urlString = [NSString stringWithFormat:@"https://google.com/search?q=%@", searchString];
NSURL *theURL =[NSURL URLWithString:urlString];

Upvotes: 0

user529758
user529758

Reputation:

Try this:

NSString *urlString = [NSString stringWithFormat:@"http://google.com?q=%@", searchString];
NSURL *theURL =[NSURL URLWithString:urlString];

Don't forget to escape urlString before passing it to NSURL if it contains spaces, special characters, etc.

Upvotes: 3

Related Questions