tags:

views:

75

answers:

2

I would like to be able to send the user out of my app and into Safari as if they had performed a search of some terms I provide. I realize I could construct a URL for doing a Google search and send them there, however the user has the option of configuring one of several search engines, and I'd like to use the one they've chosen and not Google.

Is there a way to do this?

+4  A: 

I'm pretty sure this isn't possible.

On the iPhone you launch other applications by using a custom URL scheme. A good list of these can be found here: http://wiki.akosma.com/IPhone_URL_Schemes

So for example to launch Safari:

NSString *stringURL = @"http://my.url.com/";
NSURL *url = [NSURL URLWithString:stringURL];
[[UIApplication sharedApplication] openURL:url];

The iPhone then decides to launch Safari as the URL scheme used is http. As you said in your question you can just use the correct URL to launch a search engine, so for Google:

http://www.google.com/search?q=MY_SEARCH_TERM

What would be ideal would be if Apple provided a URL scheme, such as search:// which launched the query in a search engine. However, this sadly isn't implemented.

I think the best way to "solve" your problem would be to allow the user to choose which search engine they'd like to use in your app. Then, when launching Safari you can use the appropriate URL. The added bonus is that you can also include search engines which Apple doesn't.

pheelicks
A: 

The only way to have multiple search engines implemented, would be to make a custom url every time based on what engine is selected, since every search engine have a static method of searching.

UPDATE:Made it real objective-c due to pheelicks. Also shrunk URLs.

Example:

if searchEngine == @"google" {
    url = [NSString stringWithFormat:@"https://encrypted.google.com/search?q=%@", search];
}
if searchEngine == @"bing" {
    url = [NSString stringWithFormat:@"http://www.bing.com/search?q=%@", search];
}
if searchEngine == @"yahoo" {
    url = [NSString stringWithFormat:@"http://search.yahoo.com/search?p=%@", search];
}
[[UIApplication sharedApplication] openURL:url];

and so on...

NSArray
a) Thats not even valid Objective C b) you want to be using == not = c) why do all your URLs have load of unnecessary garbage?
pheelicks
I wasn't typing Objective-C! I was just showing what it would look like while giving the url.The URLs don't load garbage, they're specifiers. ex: for google hl=en specifies english
NSArray
I think one of the issues is that I'm not sure how to determine the search engine the user chose in their preferences.
davetron5000
You could make a UISlider with however int values as search engines you have, then write a simple script that executes on touches up inside that rounds the UISlider.value to an int. You can then use a case statement that checks the search engine. If you want I can send you a sample.
NSArray
I'm sorry that doesn't make any sense; what part of that determines the search engine that the user has selected in the system preferences?
davetron5000
Base on a int value of the slider ([UISlider.value integerValue]) you determine the search engine. Again, if you want a sample I can supply you with one.
NSArray
I don't see how an int value from some random slider can tell me which search engine the user selected in the Safari Settings in the iPhone Settings application.
davetron5000
I don't think you can access those settings without using some private APIs. I was thinking about making your own settings within your app.
NSArray