UIWebView loads htmlString but the image doesn't show - html

I am trying to use UIWebView to load a html string from the web and there are lot of image src within it. The problem is the webview can load the string in the html string but the images just do not show up. Here is part of the html string, is there any problem with it?
NSString *htmlString = #"<strong>多年购置户外行装后,在她二舅的怂恿下,终于开始了第一次重装徒步。先来一张照片啊。表现我双脚走天下的决心。此次全程都是手机拍摄,终于感觉到了手机拍摄质量达不到要求的痛苦。同在蓝天下,我们的心情无比跳跃。</strong>
<div style = \"height:4px;\"> <div><img
src=\"http://210.22.129.138:801/link/api/public/media/54d8b2536580f1f2405e79f1\" style=\"height:367px; width:553px\" /> 走天下的双脚!<div style = \"height:4px;\"> <div><img
src=\"http://210.22.129.138:801/link/api/public/media/54d8b2536580f1f2405e79f3\" style=\"height:553px; width:553px\" /> ";
I use this to load the html string:
[self.webView loadHTMLString:htmlString baseURL:nil];

Try copy paste the URL on the device web browser. And check whether the browser is able to display the image. If the browser is not able to show that image then check the following
Check the correctness of the url.
Check Firewall settings. (There is a port number specified in the url, so make sure that port number is open in your firewall)
Check the IP is accessible.

Use base url :
[self.webView loadHTMLString:htmlString baseURL:[NSURL URLWithString:#"http://210.22.129.138:801/"]];

Related

How to properly write an HTML string on Objective C

I want to load a specific part of a webpage on a UIWebView
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://jlhs.schoolloop.com/mobile/login"]]];
[_webView loadHTMLString:#"<html><body><div class="slideshow_outer"></div></body></html>" baseURL:nil];
[_webView setNeedsDisplay];
The first is the website address
The second is the part of the HTML I want to load
and the third is to display it.
I get an error on the HTML string I put. How do I fix this or is there a better way of writing it?
It won't work, how do I go on with this?
// show HTML content in webview there is two different types, as follow
// 1. Using URL
// for show web view with online url
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://jlhs.schoolloop.com/mobile/login"]]];
// 2. Using string
// for show web view with help of string, local page
NSString * htmlPage = #"<html><head><meta http-equiv=\"Content-type\" content=\"text/html charset=UTF-8\" /><meta name=\"viewport\" content=\"width=device-width; initial-scale=1.0; maximum-scale=1.5; user-scalable=0;\"/></head><body style=\"font-size:13px;font-family:Arial;-webkit-user-select:none;\">Write here your HTML content </body></html>";
[_webView loadHTMLString:htmlPage baseURL:nil];
// you can't use it at a time, as per your requirement use one of these.
I hope you undersand want I want to explain.

iOS7: Display an Image in an HTML File loaded into a UIWebView

I am programming in iOS7. In my app I am using a UIWebView to load static HTML files that are packaged with the app. I want to show images, also packaged with the app, in my HTML files, but can not find the magic combination to get the images to show when the HTML file is loaded.
I saw some information on base64, which I did not understand, but I would like to find a simple solution because there will be others beside myself writing most of the HTML files.
Below is the basic format of the HTML code I am using. I have tried all manner of “/“ modifiers to no avail, including the full path as listed in the File Inspector.
<img src=“data:/Supporting Files/Info/InfoRecycle.png">
Does anyone know how I get the image to show within the HTML file?
Try putting the image in the same folder as your html file. Then reference it like this:
<img src="InfoRecycle.png">
See if that works.
I finally got it to work! I found the key to the answer in this post. I guess I did not find it earlier because I was focused on iOS7:
Using HTML and Local Images Within UIWebView
I had to make some modifications in my Xcode, so it now looks like this:
//..Loads a Local HTML File
strHTML = #"Info";
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:strHTML ofType:#"html"];
NSString* htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[_infoWebView loadHTMLString:htmlString baseURL:baseURL];
My reference to the image in the HTML code is now as suggested above by Stepan Grigoryan.
<img src="InfoRecycle.png">

WebFrame loadData not working?

I'm trying to make a WebView load a page from HTML code I have stored as an NSData. I get a blank page when I try to do this. Is there anything wrong with what I'm doing when I load the page? If not, I need to look elsewhere in my program.
if (essence.html){ //essence.html is an NSData
NSLog(#"Inserting HTML code into browser window: %#", [[NSString alloc] initWithData:essence.html encoding: NSUTF8StringEncoding]);
[webView.mainFrame loadData:essence.html MIMEType: #"text/html" textEncodingName: #"utf-8" baseURL:nil]; //webView is a WebView
}
I created the conditions so essence.html contains HTML code from the page http://kathleenmelian.com/test.html (which just says "hello"). The NSLog prints this when the above code runs:
Inserting HTML code into browser window: <html><head></head><body>hello</body></html>
So essence.html definitely contains valid code that a browser should be able to load.
You could use
[webView loadHTMLString:
[[NSString alloc] initWithData:essence.html encoding:NSUTF8StringEncoding]
baseURL:nil];
The other idea would be to replace "utf-8" with "UTF-8", which in some cases is known to make a difference (not sure about UIWebView).
Sorry to bother you guys. I fixed some other bug in my program's model, and that somehow fixed THIS problem as well. I don't know how. Crisis averted.

Convert a HTML page to a iOS View

i want to take a normal html page (a page from tf2 wiki) and convert it into a view.
For example it should take a text and put it into a label or a Image into an UIImageView.
Is there any Framework to do something like that or should i convert the site on a webserver
to make it easier for the app at runtime to render ?
Hope you understand, what i want to do.
Tim
there could be different ways depending on your scenario, you could load them in UIWebView within IOS, If you don't want show the UIWebView then you can get the html from it ans use it however you want. or other way you could transfer the HTML as json data to IOS and then show that data via UIView, UILabel or anything else. otherwise as suggested by Zakaria, you could use PhoneGap.
UPDATE with sample code for sending a request and getting its contents if you don't want to use UIWebView
//your html page with url
NSURL *url = [NSURL URLWithString:#"www.google.com"];
NSURLRequest *request=[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:30.0f];
NSOperationQueue *queue=[[NSOperationQueue alloc]init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ([data length]>0 && error==nil) {
NSString *html=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(#"this string contains all html page you have \n %#",html);
}
}
];
Not sure if i understand the question properly, but Cordova lets you develope iOS apps in html/css/js.
Check their website: http://cordova.apache.org/
If your page is a static one, then use PhoneGap : It will embed your HTML+JS+CSS in an iOS app and will be loaded in a webview.
But there is no framework that will allow you to convert your HTML elements into native ones.

How can I pull HTML code into an NSString from an iOS app?

I am trying to pull down the code from an HTML website that has no more than 2 lines on it. The code contains a word that I need to retrieve. Is there a simple way to pull down that code and put it in an NSString?
Further details: I am going to have an app that checks for a word on a page. If that word is what I am looking for, the app will show the text "confirmed". The purpose of the app is to check to see if the page is accessible.
If you need a http library to hit the server try asihttp. Apart from this i need more info of what you are trying to do...
If you just want to check if that website is reachable, you can go with HTTP Success Status Codes.
Using ASIHTTPRequest simplifies communication over the web.
If you still want to evaluate the text on that website, can also just retrieve it using:
[request responseString];
Depending on what you get from the website, it's up to you how to update the UI.
Just change the link between the quotes and it'll work!
-(void) viewDidLoad {
NSString * sFeedURL = [NSString stringWithFormat:#"http://www.google.com/ig/api?weather=,,,270000,960000"];
//RSS Feed URL goes between quotes
NSString * sActualFeed = [NSString stringWithContentsOfURL:[NSURL URLWithString:sFeedURL] encoding:1 error:nil];
NSLog(#"%#", sActualFeed);
}