Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Programmer

macrumors member
Original poster
Jun 16, 2009
79
0
could someone please help me figure out how to put a google search bar in the web browser I'm making in xcode.:eek:
 
xcode

no i'm i am trying to program it into my web browser i'm making in xcode.
 
no i'm i am trying to program it into my web browser i'm making in xcode.
How do you currently have your webView set up to get page URLs? Do you have it connected to an NSTextField via takeStringURLFrom: or are you pushing URLs to it with setMainFrameURL:?

In either case, you'll want to set yourself up as a delegate of a second NSTextField, do something like this when you receive textDidEndEditing: from it:

Code:
NSString *searchURL = [NSString stringWithFormat:@"http://www.google.com/search?q=%@", [googleField stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

Then set searchURL to your normal URL bar, possibly passing it to the WebView as well if you have the link between the two going through your own code.

The main observations are merely that you need to:

(i) escape the search string (eg, to turn a space into %20 so that it works in a URL); and
(ii) add http://www.google.com/search?q= to the start to turn it into a Google search URL.

Then just treat that identically to a URL directly entered by the user.
 
Thanks

thanks
what i'v been having trouble with is when i try to retrieve the text from the google search bar in the .h file then do the @synthesize in the .m file to put it into this string of code

HTML:
NSString *searchURL = [NSString stringWithFormat:@"http://www.google.com/search?q=%@", [GoogleSearchField stringValue]];

i am used to programing on the iphone this is what i would normally do so i don't know if it is different on a mac
 
url field problem

i'm making a web browser but when i run this code

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSString *searchURL = [NSString stringWithFormat:mad:"http://", [urlField stringValue], NSString stringWithFormat:mad:".com"];
[textField resignFirstResponder];
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[textField text]]]];


return YES;
}

i get this

HTML:
Expected expression before NSString

can anyone help
 
First (and we really need to sticky this for this forum), if you're going to include cope snippets, please enclose them in [ CODE ][ /CODE ] tags (spaces removed). These tags are easily accessed via the # icon in the toolbar.

Second, your problem starts here:
Code:
- (BOOL)textFieldShouldReturnUITextField *)textField
You probably want something like this instead:
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField

...and continues here:
Code:
NSString *searchURL = [NSString stringWithFormat:@"http://", [urlField stringValue], NSString stringWithFormat:@".com"];
You probably want something like this:
Code:
NSString *searchURL = [NSString stringWithFormat:@"http://%@%@", [textField text], @".com"];
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.