webview won't load with string - ios

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

Related

Adding built in Google search capability to browser

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

How to decode NSURL in iOS?

i am NewBie in iOS Development. I want to Decode my NSURL With Pagination Here my Url Like as
http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0
And i Decode this URL Like as
NSURL *urls = [NSURL URLWithString:#"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0"];
NSString *urlString = [urls absoluteString];
NSLog(#"UrlString %#",urlString);
NSURL * url=[NSURL URLWithString:urlString];
But it Give me Only 0 Page Url i want to Like as make my url as
NSURL *urls=[NSURL URLWithString:#"My Url &page=%d",pagenum];
Here i want at place of My Url as Decoding of my Original Url If it is Possible then Please Give me Solution for it.
you are doing it in wrong way.
this is the right way
NSString *urlString = #"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=";
NSURL *targetURL = [NSURL URLWithString:[NSString stringWithFormat:#"%#%d",urlString,pageNumber]];
NSLog(#"targetURL is : %#",targetURL);
Hope this will help you..
NSString *str=#"http://www.janvajevu.com/webservice/categorylist.php?category=%E0%AA%B8%E0%AB%8D%E0%AA%B5%E0%AA%BE%E0%AA%B8%E0%AB%8D%E0%AA%A5%E0%AA%AF&page=0";
NSString *result =[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *urls = [NSURL URLWithString:result] ;

Url not loading in UIWebview, # converting to %23

Stucked on load the below Local path url in UIWebview. Find the url in spinepath, then i converted to url [SpinePathUrl].
My issue is, when i try to load,i'm getting nil value.
For checking purpose, i pasted the SpinepathUrl into browser it show webpage not found.Then, i changed SpinePathUrl "arugoswami-4.xhtml%23toc_marker-4" into "arugoswami-4.xhtml#toc_marker-4", its showed result.
Is the # is my issue in url.? Why the # converted to %23, when it converting from String to Url.
spinepath = [NSString stringWithFormat:#"%#%#",documentsDirectory,Path];
spinepath = [spinepath stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
spinepath = [spinepath stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
[/Users/blazedream/Library/Application Support/iPhone Simulator/7.1/Applications/E19D2CD0-E0DC-460E-8BC9-97A9C8010910/Documents/UnzippedEpub/OEBPS/arugoswami-4.xhtml#toc_marker-4]
NSURL *SpinePathUrl = [NSURL fileURLWithPath:spinepath];
[file:///Users/blazedream/Library/Application%20Support/iPhone%20Simulator/7.1/Applications/E19D2CD0-E0DC-460E-8BC9-97A9C8010910/Documents/UnzippedEpub/OEBPS/arugoswami-4.xhtml%23toc_marker-4]
NSURLRequest *request = [NSURLRequest requestWithURL:SpinePathUrl];
[webView loadRequest:request];
Try adding fragment manually
NSURL *baseUrl = [NSURL fileURLWithPath:#"... arugoswami-4.xhtml"];
NSURL *fullURL = [NSURL URLWithString:#"#toc_marker-4" relativeToURL:baseUrl];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:fullURL];
[myWebView loadRequest:urlRequest];

While setting NSString to NSURL, NSURL is returning nil value

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);
}

Get title of a website silently from a website

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);
}

Resources