UIWebView and search query - url

I've start to learning UIWebView and I have next problem. When I'm trying to use next code:
NSString *searchString = [NSString stringWithFormat:#"http://www.google.com/search?q=%#",lookedCity];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:searchString]]];
or:
NSString *searchString = [NSString stringWithFormat:#"http://en.wikipedia.org/wiki/%#",lookedCity];
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:searchString]]];
my WebView doesn't loading page. But if I use standart URLs (ex. google.com), all works fine.
In case with Wiki, I tried use WikiApiObjectiveC, but even example doesn't work. How can I solve this problem? The best solution will be with Wiki URL.

Problem solved!
The reason, why my URL doesn't work, was in cyrillic symbols in my searchString. Solution: urlString = [urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

Related

webView does not load the page

I FOUND THE PROBLEM... I found that the value of berria1 have "\n " at the end of the url. where he not get this but that's the problem.
I pass very curious thing and which do not find any explanation.
The fact is that in the application, I'm parsing some news and save these in CoreData, then show a list of news in a UITableView and if I click on one of them brings me to a UIWebView in which position the link with the full story. Now comes the weird ...
If I pass to NSURL the variable which I recovery of CoreData which contains the Web address, do not load, if I pass the same direction to a NSString and this NSString to UIWebView, the UIWebView load this normally. The following is my code.
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.webView.delegate = self;
NSString *berria1 = [[NSString alloc]init];
berria1 = [NSString stringWithFormat:#"%#",self.abisua.noticia];
NSString *berria2 = #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";
NSURL *url = [NSURL URLWithString:berria1];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
NSLog(#"El contenido de la noticia 1 es %#", berria1);
NSLog(#"El contenido de la noticia 2 es %#", berria2);
}
With the NSLog I see that the values of the variables are the same as you see in the picture below.
The value of the two variables in the NSLog
Image - Debug window - URL is nil
That could be happening?
Thank you.
Its not berria1 which holds the URL its berria2.
NSURL *url = [NSURL URLWithString:berria2];
Here you go give it a try the site is pretty slow so it takes a time to load
NSString *berria2 = #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?%20%20%20%20idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9";
NSURL *myUrl = [NSURL URLWithString:berria2];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
[self.webView loadRequest:myRequest];
Try this. I didn't see any code how you load the request to webview. But I added the code with that part. Replace it with your webview.
NSString *myurlString = [NSString stringWithFormat:#"%#",self.abisua.noticia];
NSString *cleanedUrl = [myurlString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
NSURL *myUrl = [NSURL URLWithString:cleanedUrl];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myUrl];
UIWebView *mywebview = [[UIWebView alloc] init];
[mywebview loadRequest:myRequest];
I finally found and fixed the problem.
The problem was that the variable received whith Core Data and pass to NSString, I do not know how, but the link have added this characters at the end of URL "\n "
Berria1 variable value in debug area was:
berria1 __NSCFString * #"http://www.vitoria-gasteiz.org/we001/was/we001Action.do?idioma=eu&aplicacion=wb021&tabla=contenido&uid=u_47906a5c_14eabe80aeb__7fe9\n " 0x0000000126d9e6c0
Then when I passed this value to NSURL and this found these characters, NSURL value pass to nil.
Then I solved this, passing this NSString to a NSMutableString and erasing these characters before pass to NSURL with this code:
NSString *berria1 = self.abisua.noticia;
NSMutableString * miCadena = [NSMutableString stringWithString: berria1];
[miCadena deleteCharactersInRange: [miCadena rangeOfString: #"\n "]];
Thanks everyone for your help
hi am new to iOS developer,i have seen your code in that your adding second string to url why still understand just try adding 2nd string
NSURL *url = [NSURL URLWithString:berria2];

Can't load javascript in UIWebView

I'm trying to load a javascript file but it doesn't work.
My code :
NSString *html = [NSString stringWithFormat:#"<html><head></head><body><script type=\"text/javascript\" src=\"http://mywebsite.com/file.js\"></script></body></html>"];
[_webView loadHTMLString:html baseURL:nil];
This code is supposed to display a video, but nothing happens.
This code displays correctly the video in Desktop browser, my JS is correct.
use url in baseURL
NSURL *base=[NSURL URLWithString:#"website.com"];
Do not use string description. Instead use:
[_webView loadHTMLString:html baseURL:nil];
Write following code in - webViewDidFinishLoad:
NSString * strJavaScript = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://mywebsite.com/file.js"] encoding:NSUTF8StringEncoding error:nil];
[_webView stringByEvaluatingJavaScriptFromString:strJavaScript];

UIWebView not displaying URL

Pretty easy enough problem, but It's driving me crazy. I connected my uiwebview to the .h and called it myWebView. then in the viewdidLoad, i did this:
NSString *urlString = #"www.google.com";
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[myWebView loadRequest:request];
But when I run the app I just see a blank screen...
I don't know what's going on.
All help is appreciated, thanks
Your urlstring isn't well-formed (for your intent)
Use http://www.google.com/
Technically www.google.com is a valid URL (according to things like RFC 1738 and 3986), but since it lacks the scheme (the http part), the UIWebView doesn't quite know what to do with it.

Parse RTF in Objective c

I want to parse RTF file in Objective c.Is its possible to parse it? if is possible then how can I use it into IOS.I want all the text and formatting.
I have tried in so many way to get text and formatting for this purpose I have used UIWEBVIEW but its not return any text string
NSURL *imgPath = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"rtf"];
NSData *data = [NSData dataWithContentsOfURL:imgPath];
[webView loadData:data MIMEType:#"text/rtf" textEncodingName:#"utf-8" baseURL:[NSURL URLWithString:#"/"]];
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
Here html return nil value.
Thanks
Ok, this question is very old but in case someone stumbles across this problem:
The html is nil as the webView is not finished loading. Fetch the html in the UIWebViewDelegate:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *html = [webView stringByEvaluatingJavaScriptFromString: #"document.body.innerHTML"];
NSLog(#"Html: %#",html);
}
You will get the desired html.

Load WebView with Variable

I am trying to accomplish a very simple task of loading a WebView that has a variable. I am hoping to pass the variable from objective-c to a remote PHP file. The code I am using does not seem to work. The variable is valid, but I cannot get it to pass to the PHP file to be read by the WebView. Any help would be great!
NSString *userId = [[NSUserDefaults standardUserDefaults]
stringForKey:#"userId"];
[webViewFirst loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.website.com/page.php?userId=%#",userId]]];
Thank you very much!
Try +[NSString stringWithFormat] to build the URL string:
NSString *userId = <#whatever#>;
NSString *link = [NSString stringWithFormat:#"http://www.website.com/page.php?userId=%#", userId]
[webViewFirst loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:link]];
NSURL URLWithString does not support string formatting, so you cannot use it like you are trying to do.
What happens, instead, is that the C , operator is used, which just sequences the two expressions: #"http://www.website.com/page.php?userId=%#" and userId, and evaluates to the last one.
Use stringWithFormat to get it right:
[webViewFirst loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:#"http://www.website.com/page.php?userId=%#",userId]]]];

Resources