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

nagol2013

macrumors newbie
Original poster
Jun 10, 2011
6
0
Hi, I am working on an application and part of it is to let the user type something in a text field and search it via google. Right now I have it linked to google and when the user presses search its transitions to the webView. My problem is that I can't get xcode to take what the user has typed into the text field and insert it into the google link properly. Right now it just shows a search of "(null)" in the webView. I was wondering if anyone can help me resolve this. Oh by the way this is my first post and I don't know how to put code in properly so I will just copy and paste it.

Code:
 NSLog(@"%@",searchBarGoogle.text);
 NSString *string1 = [[NSString alloc] initWithFormat:@"%@",searchBarGoogle.text];
 NSString *string = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",string1];
 NSURL *url = [NSURL URLWithString:string];
 NSURLRequest *request = [NSURLRequest requestWithURL:url];
 [webView loadRequest:request];

Thanks in advance for future help!
 
Last edited by a moderator:
Oh by the way this is my first post and I don't know how to put code in properly so I will just copy and paste it.

Here is the URL for this forum (iPhone/iPad Programming):
https://forums.macrumors.com/forums/135/

Click it.

At the top of the Threads list are some threads marked Sticky.

Click the one named "Posting code, please use the
Code:
 tags".

Follow its instructions.


[CODE]NSString *string = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",string1];
After this statement, NSLog the values of both string and string1. Post the output.

If I had to guess, I would guess that string1 has some characters that are illegal in URLs, and you haven't percent-escaped them. There is an NSString method that will percent-escape things. Look it up in the NSString class reference doc.

Oh, and it looks like you might be leaking string1.
 
Last edited:
is it possible to do an if statement to say that if it returns null then put the user's input?
 
is it possible to do an if statement to say that if it returns null then put the user's input?

Do you know how to write an if statement?

What book or tutorial are you using? Be specific.
Title, author, and edition for a book; website and URL for an online tutorial.


What have you tried? Be specific.

1. Post your code, even if it doesn't work.
2. Describe what you expected to happen.
3. Describe what actually happened.
4. Post any error or warning messages, exactly.


When describing problems, be specific and accurate. I don't know what "it" means where you wrote "if it returns null". I could guess you mean the user's input, but you haven't identified which variables are user inputs or what their types are. Also, "if it returns null" doesn't make logical sense here. What does make sense is "if the user input is NOT null", but that's the opposite of what you wrote. Accuracy is important.
 
Also, be aware that your thread title is a little misleading, as is this statement:
My problem is that I can't get xcode to take what the user has typed into the text field and insert it into the google link properly.

The problem is not with Xcode, which is just a tool to work with your code; the problem lies with Objective-C, and your understanding of how to use it.

Hope that helps.
 
I think you need to redefine your questions more properly, for us to debug it.
Code:
 NSLog(@"%@",searchBarGoogle.text);
 NSString *string1 = [[NSString alloc] initWithFormat:@"%@",searchBarGoogle.text];
 NSString *string = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",string1];
 NSURL *url = [NSURL URLWithString:string];
 NSURLRequest *request = [NSURLRequest requestWithURL:url];
 [webView loadRequest:request];

first u log your searchBarGoogle.text -> I guess that's an UITextField.
But if it's nil here, then that's the first error. because after you are making a new string (imo, just use [NSString stringWithFormat:] like the one below your second line. This is autoreleased, and will cause u less headeaches.
The URL creating is correct, and loading it into a URL too.
The problem is purely based on your textfield, and getting the right info from there.

So, how and when do you do that ;)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.