I am trying to give my simple browser the ability to recognize that text with spaces should be treated as a google search query. To create a string that can go into a url I need to replace the spaces with "+"s.
I have attempted to do this in the first "if" statement however when I do run the program and put text with spaces into the search bar nothing happens; entering "google.com" or other urls work though.
What is wrong with my code?
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
NSString *URLString = textField.text;
NSURL *URL = [NSURL URLWithString:URLString];
if ([URLString rangeOfString:#" "].location != NSNotFound) {
NSString *plusReplace = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
URL = [NSURL URLWithString:[NSString stringWithFormat:#"google.com/search?q=<%# query", plusReplace]];
}
if (!URL.scheme) {
// The user didn't type http: or https:
URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", URLString]];
}
if (URL) {
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
}
return NO;
}
I think you might have an issue with the 3rd conditional making a 2nd request. What if you change it to this?
if (URL) {
NSString *plusReplace = [URLString stringByReplacingOccurrencesOfString:#" " withString:#"+"];
URL = [NSURL URLWithString:[NSString stringWithFormat:#"google.com/search?q=<%# query", plusReplace]];
}
if (!URL.scheme) {
// The user didn't type http: or https:
URL = [NSURL URLWithString:[NSString stringWithFormat:#"http://%#", URLString]];
}
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
[self.webView loadRequest:request];
Related
I have to parse the return of a JSON API for which I've taken the URL into a string and after that I passed it NSURL but in NSLog it is showing nil.
Here is my code,
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSURL *url=[[NSURL alloc]initWithString:urlstring ];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
Thanks in advance.
You must encode your url as it contains special characters, Try something like this
NSString *paramString=[NSString stringWithFormat:#"/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[self.strurl lowercaseString]];
NSString *encodedSearchString = [paramString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *urlString = [NSString stringWithFormat:#"http://api.v3.factual.com%#", encodedSearchString];
NSURL *url = [NSURL URLWithString:urlString];
NSLog(#"urlstring is %#",url)
Hope this helps
Try this, Tested and Working
-(void) sample
{
// NSString *strurl = #"hello";
NSString *urlstring=[NSString stringWithFormat:#"http://api.v3.factual.com/t/places?q=%#&geo={\"$circle\":{\"$center\":[19.9909631,73.8034808],\"$meters\":40000}}&limit=20&KEY=123456",[strurl lowercaseString]];
NSURL *url=[NSURL URLWithString:[urlstring stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSLog(#"%#",req);
}
In my webview viewdidload i'm implementing the following code. the problem is the webview won't appear when I include the NSString *encodedString. I have searchString with spaces and sometimes &. Any ideas on what's going on here?
NSString *urlString = [NSString stringWithFormat:#"http://google.com?q=%#", searchString];
NSString * encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(
NULL,(CFStringRef)urlString,
NULL,(CFStringRef)#"!*'();:#&=+$,/?%#[]",
kCFStringEncodingUTF8 ));
NSLog(#"searchString is %#", searchString);
NSURL *myURL =[NSURL URLWithString:encodedString];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[searchWebView loadRequest:myRequest];
What would it take to generate the first page results of google for this searchString?
Try This
NSString *urlString = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",searchString];
urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *myURL =[NSURL URLWithString:urlString];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[searchWebView loadRequest:myRequest];
Use this to encode Your SearchString
SearchString=[[NSString stringWithFormat:#"%#", SearchString] stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];
and then append it to your URL
I have a link of a website, and I want to get its title.
I tried to do by this code
UIWebView* hiddenWebView;
NSString* urlString = #"http://www.youtube.com/watch?v=OyORxdjGtlk";
NSURL* url = [NSURL URLWithString:urlString];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[hiddenWebView loadRequest:request];
NSString* text = [hiddenWebView stringByEvaluatingJavaScriptFromString:#"document.title"];
But the result is: text = NULL;
I just want to get the name of the video
Set the UIWebView delegate to your ViewController (for example by ctrl-dragging in Interface Builder) and then:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSURL *url = [webView.request mainDocumentURL];
NSString *str = [url absoluteString];
NSString *title = [webView stringByEvaluatingJavaScriptFromString:#"document.title"];
NSLog(#"%s: url=%# str=%# title=%#", __PRETTY_FUNCTION__, url, str, title);
}
I would to follow any user with the help of my application. Can anybody suggest me what to do ? As I read the Instagram API from here. But not getting proper idea what to do.
You can do this way :-
NSString *urlString=[NSString stringWithFormat:#"https://api.instagram.com/v1/users/%#/relationship?access_token=%#",<user id>,<access token>];
NSURL* url = [NSURL URLWithString:urlString];
theRequest = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:1000.0];
NSString *parameters=#"action=follow";
[theRequest setHTTPBody:[parameters dataUsingEncoding:NSUTF8StringEncoding]];
[theRequest setHTTPMethod:#"POST"];
To follow we only need client_id
First of all create a client_id by registering your app on Instagram
and open given Url in webView.
NSString *urlAddress =[NSString stringWithFormat:#"https://instagram.com/oauth/authorize/?client_id=%#&redirect_uri=http://appbellfitness.com/&response_type=token&scope=likes+comments+relationships",client_id];
NSURL *nsurl=[NSURL URLWithString:urlAddress];
NSURLRequest *nsrequest=[NSURLRequest requestWithURL:nsurl];
[webview loadRequest:nsrequest];
webview.delegate = self;
And add WebView delegate to it.
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSString *orignalUrl =[request.URL absoluteString];
if ([orignalUrl hasPrefix:CALLBACKURL])
{
NSArray* parts = [orignalUrl componentsSeparatedByString: #"="];
request_token = [parts objectAtIndex: 1];
NSString *myurl =[NSString stringWithFormat:#"https://api.instagram.com/v1/users/25025320/relationship?access_token=%#",request_token];
NSString *action=#"action=follow";
BsyncTask *task = [[BsyncTask alloc]init];
[task asynchronousPost:action url:myurl callerName:#"followTask"];//post this as you like
task.recieveAsyncResponse = self;
}
return YES;
}
shouldStartLoadWithRequest
I check it starts with my redirect Uri then I get the access token by seperating the url on the basis of = sign. and then i post it using my class you can post it as you like and get response.
I have an application at the moment that basically opens a url depending on what the user has typed into a textbox. The logic for this is as such:
Predetermined beginning + User Input + Predetermined End
So basically my URL is 3 concatenated strings. Now I know the link is being formed properly (I've put the same snip of code into a label) but nothing happens when I press the button to load the webview.
It works perfectly fine when I use the below, where I explicitly type https://google.com
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = #"https://google.com";
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
However nothing happens when I use this code which includes my concatenated url:
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}
Any help would be much appreciated.
UPDATE: I'm fairly sure having the stringWithFormat is causing this problem just unsure how to get around it.:
Check if fullURL starts with http:// or https://, if not prefix that.
- (IBAction)btnSearchPress:(id)sender {
[self.view endEditing:YES];
[super viewDidLoad];
NSString *fullURL = [NSString stringWithFormat:#"%#%#%#", _urlPrefix.text,_txtInput.text, _urlSuffix.text];
if ( ! ([fullURL hasPrefix:#"http://"] || [ fullURL hasPrefix:#"https://"]) ) {
fullURL = [NSString stringWithFormat:#"http://%#", fullURL ];
}
NSURL *url = [NSURL URLWithString:fullURL];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[_wbView loadRequest:requestObj];
}