How to implement google search application in iphone - iphone

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];

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.

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;

Related

IOS - UIWebview - Get the URL of the link clicked and edit the url

I'm creating an app which has a webview. When user click to a link or a button in the webview, I want to be able to get the new url, and edit the new url.
First I initialize the webview with the following url:
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
}
When I click to a link, for example www.website.com/contact.php
Then I edit the new url with www.website.com/contact.php*?id=12212323*
I need to keep the id parameter in all the urls.
The UIWebViewDelegateProtocol should suit your requirements.
In particular the method - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
this is a code to get the current URL
- (void)viewDidLoad
{
NSString *id = #"12212323";
[super viewDidLoad];
NSString *fullURL = [[NSString alloc] initWithFormat:#"www.website.com/index.php?id=%#", id];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[webView loadRequest:requestObj];
NSURL *currentURL = [requestObj URL];
NSLog(#"Current URL is %#", currentURL.absoluteString);
}
but to create a URL bar hidden or not is a lot of code, you can start from this tutorial on YouTube is simple and work very well, is possible give you other idea to make a good job ;)
Web Browser Tutorial
hope this help you

UIWebView Load URL with parameters

I have an application that stores the user ID and code in NSUSERDEFAULTS I need to use these values in order to get UIWebView to visit url: www.mywebsite.com/check.php?id=(idhere)&code=(idhere).
I have looked at some other topics but nothing has seemed to help so far.
I'm new to this language so thanks for your patience.
Jack Brown.
Simplest form:
- (void) viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [UIWebView alloc] initWithFrame:self.view.frame];
[self.view addSubview:webView];
NSString *url = #"http://google.com?get=something&...";
NSURL *nsUrl = [NSURL URLWithString:url];
NSURLRequest *request = [NSURLRequest requestWithURL:nsUrl cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30];
[webView loadRequest:request];
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *userId= [prefs stringForKey:#"userIdkey"];
NSString *code= [prefs stringForKey:#"codeKey"];
and so your url string will be,
NSString *urlStr = [NSString stringWithFormat:#"http://www.mywebsite.com/check.php?id=%#&code=%#",userId,code];

create a UIWebView and load a website programmatically

The following is my code. I simply want to load a website page and put a back button on screen. Don't know why nothing shows on the screen.
in .h
#import <UIKit/UIKit.h>
#interface ThirdViewController : UIViewController
{
UIWebView *myWebView;
}
#property (nonatomic, retain) UIWebView *myWebView;
-(IBAction)goBack:(id)sender;
#end
in .m
#import "ThirdViewController.h"
#implementation ThirdViewController
#synthesize myWebView;
-(IBAction)goBack:(id)sender
{
[myWebView goBack];
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAddress = #"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:requestObj];
[self.view addSubview:myWebView];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(goBack:)];
}
Initialize your webView.
UIWebView *tempWebview = [[UIWebView alloc]initWithFrame:theFrame];
NSString *urlAddress = #"http://www.apple.com";
NSURL *url = [NSURL URLWithString:urlAddress];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
self.myWebView = tempWebview;
[tempWebview loadRequest:requestObj];
[tempWebview release];
myWebView.delegate=self;
First Initialize Web view .Write The Below Code In Between [Super
ViewDidLoad]; and Nsstring Inisilization.
myWebView=[[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myWebView.delegate=self;
[self.view addSubview:myWebView]
Then Load Request Your Code Will Work.
Add the below code into your viewcontroller.m file viewDidLoad method.
UIWebView *webView = [[UIWebView alloc]init];
NSString *urlString = #"http://www.sourcefreeze.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[self.view addSubview:webView];
for complete uiwebview example please refer the below link.
https://sourcefreeze.com/ios-webview-uiwebview-example/

url not loading in UIWebView

i have a tab bar control. 1st tab contains a navigation control. On the 2nd tab i want to load a web page (say google.com). I have written the code as
NPIViewController.h
#interface NPIViewController : UIViewController {
IBOutlet UIWebView *webView;
}
#property (nonatomic,retain) IBOutlet UIWebView *webView;
#end
NPIViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
The page just does not load. No compilation or runtime errors. What is wrong in this ?
to know whats wrong you can do this.
- (void)viewDidLoad {
[super viewDidLoad];
webView.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
add this new method in your class
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
NSLog(#"Error : %#",error);
}
I hope you have connected webView object with its outlet in Interface builder?
Thanks,
You have to use https instead of http. If you use http you will get this error message (use Ravin's code to see the error):
"The resource could not be loaded because the App Transport Security policy requires the use of a secure connection."
Create you webView
IBOutlet UIWebView *webView;
Try this code
NSString *urlAddress = #"http://www.google.com";
//Create a URL object.
NSURL *url = [NSURL URLWithString:urlAddress];
//URL Requst Object
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
//Load the request in the UIWebView.
[webView loadRequest:requestObj];
Restart your IOS simulator.
It is really not evident, but at first check some site in Safari at IOS simulator.
After restart of IOS simulator, my webView opened successfully both at simulator and device.
See this famous link.

Retrieve data from website database+how to

I have made a project and when people put a keyword into UITextfield and click the button then my app will retrieve the data from website database.
It is not working here is the code:
- (IBAction) btnClickMe_Clicked:(id)sender {
NSString *kw = s.text; // <-----THIS IS THE UITextField
NSURL *url = [NSURL URLWithString:#"http://www.example.com/index.php?keyword=",kw];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[iMessageLabel loadRequest:request];
}
Any one could help me?
You aren't putting the keyword into the string. You need something like this:
NSString *kw = s.text;
NSString *encodedkw = [ky stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat: #"http://www.example.com/index.php?keyword=%#", encodedkw];
NSURL *url = [NSURL URLWithString:urlString];
Then look at how to use NSURLConnection to figure out how to put the data into the label.