The 'search' function on my textfield keyboard isn't working - iphone

-(IBAction)searchInfo: (id)sender
{
NSString *query = [googleBar.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.co.uk/search?q=%#" , query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[textField resignFirstResponder];
NSString *javascriptString = #"document.getElementsByClassName('g')[0].getElementsByTagName('a')[0].click()";
[webView stringByEvaluatingJavaScriptFromString: javascriptString];
}
So basically my 'Searchinfo' button searches whatever the users inputs into the textfield on Google and then displays the first search result. When the user is editing the textfield using the pop up keyboard, I've noticed that the keyboard's search function dosen't do the same thing as the 'Searchinfo' button. Here is my code for the keyboard;
- (IBAction)ReturnKeyButton:(id)sender
{
[sender resignFirstResponder];
}
I have wired everything but I'm not sure why when I resign the keyboard using its search function it doesn't do the same as my button above, this being it taking the users input in the textfield and searching it onto Google and displaying the first search result.

If you want your return key to cause the same action as the searchInfo button, you probably want to call your searchInfo method from returnKeyButton
eg:
-(IBAction) returnKeyButton:(id) sender {
[self searchInfo:sender];
[self resignFirstResponder];
}

Related

Open url with variable syntax

I have a variable agencyWebsite and a label that should open the website when clicked with this method:
- (void)website1LblTapped {
NSURL *url = [NSURL URLWithString:self.agencyWebsite];
[[UIApplication sharedApplication] openURL:url];
}
I get a warning in the compiler saying:
Incompatible pointer types sending UILabel* to parameter of type NSString*
And the app crashes when the link is clicked. Any suggestions?
Edit: Here is what I am doing to make the label clickable
UITapGestureRecognizer* website1LblGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(website1LblTapped)];
// if labelView is not set userInteractionEnabled, you must do so
[self.agencyWebsite setUserInteractionEnabled:YES];
[self.agencyWebsite addGestureRecognizer:website1LblGesture];
What i used to get it working
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", self.agencyWebsite.text]];
If agencyWebsite is of type UILabel*, you need to access its text property instead of passing the object itself to URLWithString:.
- (void)website1LblTapped {
NSURL *url = [NSURL URLWithString:self.agencyWebsite.text];
[[UIApplication sharedApplication] openURL:url];
}
Calling self.agencyWebsite will return your UILabel* object, whereas self.agencyWebsite.text will return a NSString* object containing the text from the label.

How to make url and google bar into one UIWebView

in the new safari on the mac they have a bar that can distinguish between google and a url.Thats what i am trying to do. I am wondering what i am doing wrong. It only wants to google not search url. Thanks in advance.
-(IBAction)SearchAll:(id)sender
{
if (googlebar) {
NSString *query = [googlebar.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.google.co./search?q=%#", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
else{
NSString *query = [googlebar.text stringByReplacingOccurrencesOfString:#" " withString:#"+"];
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:#"http://www.%#", query]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];
}
}
Firstly, your code is way off. You will not achieve your goal this way.
Secondly, You should start off your IBAction statement by checking if the string that was input into the search bar (or urlbar whatever you want to call it) has a prefix of .com or .org...etc. If it does not then it then google searches the text in the UITextfield.
are you sure of your if statement?
you're using searchbar in the else, where searchbar if supposed not to exist.
you should also check the google.co./search url. I'd use google.com/search as it will save you a redirect
I'm still confused by your question though, as you have two toolbars: searchbar and googlebar, not one as in the latest safari for mac

Clickable link in ios?

In my app i am using UITextView to display three lines. In that i need to add link for certain text. I am searched in google and SO, but i couldn't found the exact solution for my problem.
I Tried this but its not working
NSString *testString = #"This will go to Google";
[webview loadHTMLString:testString baseURL:baseURL];
[lblLinksTitle setText:testString];
I want Google as clickable text which have to open in safari browser. May be its simple. As i am new to iOS i couldn't figure out to do this.
Thanks for your help guys.
Much appreciated.
Just use UIWebView, don't use UiTextView
NSString *testString = #"This will go to Google";
[webview loadHTMLString:testString baseURL:nil];
webview.delegate=self;
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
NSString * requestString=[[request URL] absoluteString];
//NSLog(#"%# is requestString from clicking",requestString );
if ([[[request URL] absoluteString] hasPrefix:#"http://www.google"]) {
[[UIApplication sharedApplication] openURL:[request URL]];
}
return TRUE;
}
Try using this:
lblLinksTitle.editable = NO;
lblLinksTitle.dataDetectorTypes = UIDataDetectorTypeLink;
lblLinksTitle.text = testString;

Modify URL request in shouldStartLoadWithRequest method and displaying in Webview

I have one webview and 3 url.
So when application starts i am showing URL1 in webview.
Now when i select any portion of webview it will redirect to URL2.
But only i want to fetch some data from URL2 and dont want to show it to user.
Which i can able to do by using shouldStartLoadWithRequest: method with return NO.
but Now i need to show URL 3 with data received from URL2 in my Webview.
But it is not showing anything ,how can i do it ?
For this i am using following code
-(void)viewDidLoad
{
//Normal showing of URL1 in webview
}
- (BOOL)webView:(UIWebView*)webViewRef shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
{
if(selectedDataExist){
//get data from URL2
//Make New URL3 string
[webView loadRequest: [NSURLRequest requestWithURL:[NSURL URLWithString:myNewUrlString]]];
return NO;
}
else
{
//by default URL1 comes
return YES;
}
i did this:
i was trying to send a info via GET method so i substring the last 10 characters (in my case) of any url requested, if it doesn't have the GET method, it make a new request appending the GET method to the url and return NO, next time this function get called it will have the GET method so it will continue.
-(BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
NSString *urls = [request.URL absoluteString];
NSString *code = [urls substringFromIndex: [urls length] - 10];
if (![code isEqualToString:#"?iphone=si"]) {
NSURL *nueva = [NSURL URLWithString:[NSString stringWithFormat:#"%#?iphone=si",urls]];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:nueva];
[self.mywebview loadRequest:requestObj];
return NO;
}
return YES;
}
This is how I do it
NSURL *LocalUrl = [[NSURL alloc] initWithString:[newUrl stringByAddingPercentEscapesUsingEncoding: NSASCIIStringEncoding]];
NSURLRequest *objNSURLRequest;
objNSURLRequest = [NSURLRequest requestWithURL:LocalUrl];
[yourwebview loadRequest:ObjNSURLRequest];
[LocalUrl release];
return NO;

Text as Hyperlink in Objective-C

I have a text label with the text set as Contact Us.. When user taps on this label it should launch safari to open the web page.My doubt is how to make Contact Us as hyperlink.Now I can't modify my code to include a UIWebView..Please help me guys..I am in final stages of my project..If possible please help me with sample code..thanks for all your time
The easiest way is to make the UILabel into a UIButton, style it (use Custom type to get rid of button look). Then connect to an Action that opens safari.
The action should do this:
NSURL *url = [[[ NSURL alloc ] initWithString: #"http://www.example.com" ] autorelease];
[[UIApplication sharedApplication] openURL:url];
for XOS use NSWorkspace instead:
- (IBAction)btnPressed:(id)sender {
NSURL *url = [[NSURL alloc] initWithString: #"https://www.google.com"];
[[NSWorkspace sharedWorkspace] openURL:url];
}