4

I am new to iPhone development. I am making an application where I need to implement Google Search application and display the results below...

Can anyone please help if there is any tutorial to implement this application. Is there any tutorial?
I think the code given below is right, if so then what else should be added to make it work?

Thank you

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];
Sam Spencer
  • 8,492
  • 12
  • 76
  • 133
Abhilash
  • 638
  • 4
  • 11
  • 28

2 Answers2

5

in your .h file

#import <UIKit/UIKit.h>

@interface eddwedViewController : UIViewController {
    IBOutlet UITextField *searchtextField;

}
-(IBAction)search;

@end

In your .m file

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
        return YES;
}

-(IBAction)search
{
    NSString *searchString=searchtextField.text;    
    NSString *urlAddress = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",searchString];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlAddress]];
}

In your view controller.xib make a textfield connect delegate with file owners and outlet with its name. Make a button and connect it action with search method.

Let me know if it is working for you.

Gypsa
  • 11,230
  • 6
  • 44
  • 82
  • 1
    @gypsa thank u....yup this worked...but when i click button its going to google page and opening...i wan the result to be displayed in same page where i enter the string in textField...below tat itself i want result to be displayed.. – Abhilash Jun 21 '11 at 04:33
  • @abhilash ok so wnat you have to do is create an new view controller and adda web view in it.Lets say you name it webVC.h now in your -(IBAction)search method navigate to that page and write NSString *searchString=@"apple"; NSString *urlAddress = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",searchString]; NSURL *url = [NSURL URLWithString:urlAddress]; NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; [webView loadRequest:requestObj]; code on your viewDidLoad. – Gypsa Jun 21 '11 at 04:50
  • @Abhilash dont hesitate to accept answers and voting up.It will increase your contribution on SO. – Gypsa Jun 21 '11 at 04:51
  • @gypsa thanks a lot..ya sure i ill accept the answer...it helped me a lot.....thank u and if u can clear this doubt as well i ill send u the link – Abhilash Jun 21 '11 at 05:02
  • @gypsa http://stackoverflow.com/questions/6407789/how-to-display-results-from-a-method-in-uilabel – Abhilash Jun 21 '11 at 05:02
  • above answer is bit confusing me...i wan to display in same page but y to hv another viewController class?cant we have webview in same UI below the button? – Abhilash Jun 21 '11 at 05:18
  • @abhilash yes you can have another view below and on that button you can have you view hidden to NO. – Gypsa Jun 21 '11 at 05:25
  • the link you provided is very complicated i dont think you requirement is that. – Gypsa Jun 21 '11 at 05:29
  • Check this answer http://stackoverflow.com/questions/8088473/how-do-i-url-encode-a-string#answer-8088484 – Khaled Annajar May 18 '15 at 15:28
1

try this:-

    NSString *searchString=@"apple";    
    NSString *urlAddress = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",searchString];
    NSURL *url = [NSURL URLWithString:urlAddress];
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    [webView loadRequest:requestObj];

It is working fine.Hope It will help you.

what you can do is:-

NSString *searchString = searchBarGoogle.text;
Gypsa
  • 11,230
  • 6
  • 44
  • 82
  • thank u...in UI i need to add textfield,webview and searchbar rite?and wat all other methods i need to implement in code?i am new to iphone i am not so good at coding...if u can help would be much better.. – Abhilash Jun 20 '11 at 11:11
  • yes you have to add these and you have to implement search bar delegate methods and textfield delegate methods. – Gypsa Jun 20 '11 at 11:18
  • i tried following tutorial http://www.totagogo.com/2011/02/08/google-local-search-ios-code/ but its for map application....can we modify this tutorial and implement for search application? which is easier above method or the tutorial? – Abhilash Jun 20 '11 at 11:26
  • can u tell what you exactly want to you.What is your requirement of this page. – Gypsa Jun 20 '11 at 11:41
  • I think you nno need to implement search bar just simply add a tesxfield and when keyboard button clicked open your google . – Gypsa Jun 20 '11 at 11:42
  • @gypsa ok my requirement is in UI i need to have textfield and a button...when i enter someth like "apple" in the text field and press the button it should search from google and display minimum 4 results below...so plz tel me wat all i shd add in UI and give me a code to implement.. i just started with iphone just month a back so i am not so gud at coding part tats the issue... – Abhilash Jun 20 '11 at 11:47