Reputation: 181
I want a Google web search to open in a Safari for a certain search term, that is set by the app. I am aware of the openURL method and have used it but I can't get a custom search opened in Safari.
This is the code I've got so far
NSString *searchTerm = [NSString stringWithFormat:@"Soccer World Cup"];
searchTerm = [searchTerm stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSString *url = [NSString stringWithFormat:@"http://www.google.com/#sclient=psy&hl=en&site=&source=hp&q=%@&aq=f&aqi=&aql=&oq=&gs_rfai=&pbx=1&fp=a20cfd04ba3c5cf9",searchTerm];
NSURL *googleURL = [NSURL URLWithString:url];
if(![[UIApplication sharedApplication] openURL:googleURL]){
NSLog(@"Failed to open URL");
}
This code ends up opening google in Safari but with no search term
Any help will be appreciated! Thanks
Upvotes: 2
Views: 1606
Reputation: 8734
Looks to me like your url is incorrect. Open up Safari on your Iphone and do a search in the google search field. Copy the url from that. To be more specific, your url should probably begin like this: http://www.google.com/search?
(note the question mark, that signals the beginning of the parameters in your url). Each parameter is then a name-value pair like for instance source=hp
in your own code example. These pairs are separated with the &
character. Most of the parameters in your code example can probably be skipped.
Second, you are going to need to replace more than just spaces in your search term (you need to urlencode it).
Upvotes: 1
Reputation: 32377
#
defines a URL fragment, and is typically used to jump to a specific named anchor on the page. Some servers will process that info, but it is rare.
You want to use a ?
instead to start your URL query parameters
http://www.google.com/?sclient=....
Upvotes: 3