UIWebView locally save images and just load html - iphone

i want to load just the html from the web and show the images from the local resource.
This helped me a lot, but is the path to the resource on every device the same? Or does it depend on an installation path or similar?
Load Content in the UIWebView on the Iphone

the path to resources is different on every device, that's why you use the function
[[NSBundle mainBundle] pathForResource:nameOfImage ofType:#"jpg"]
the html loading from the web you can do all kinds of ways. The easiest is just:
NSString *myHTML = [NSString stringWithContentsOfURL:[NSURL URLWithString:#"http://web.site.com/file.html"]];

Related

HTML file creation with image

I created an HTML file programmatically with images sources. The images are stored in the documents directory. The problem is that when I give the image source path as document directory path the images are not shown in Windows.
So, I choose the second option is to give only the image's name instead of whole path. Then it work well on Mac & Windows, but now images are not shown on the device's UIWebView.
Then I selected to open the HTML file in Safari insted of UIWebView, but the safari could not be opened because URL becomes NULL.
My code to open Safari is as follow:
NSLog(#"%#",documentsDirectory);
NSString *urlStr = [NSString stringWithFormat:#"%#/AccReport1.html",documentsDirectory];
NSLog(#"urlStr :: %#",urlStr);
NSURL *url1=[[NSURL alloc]init ];
url1 = [NSURL fileURLWithPath:urlStr];
NSLog(#"url :: %#",url1);
[[UIApplication sharedApplication]openURL:url1];
I want to mail the HTML file that contains images. And these HTML file and images are stored in document directory. I want to show this HTML file with images in windows browser, mac browser and iphone's safari.
How to do this?
One of the simple way to solve your problem is to keep Images and HTML file in same directory and refer those images in HTML files just by name and if you put them in a folder then give the complete path.
The images are not showing because the image path specified within the HTML code is not the documents directory path.
I suggest you might have to construct an html string and then subject the uiwebview to load that string
NSString *htmlString = [NSString stringWithFormat:#"<html><body><img src=\" %#/image.png \"></img></body></html>",documentsDirectoryPath];
[webView loadHTMLString:htmlString baseURL:nil];

Pick image from resource and load into web view

I have a image say point.png in folder ABC in resourse.
I want to show that image in html
NSString *myFilePath= [[NSBundle mainBundle] pathForResource:#"Point1" ofType:#"png"];
NSMutableString *strForWebView = [NSString stringWithFormat:#"<html>"
"<head><b>%#</b></head><br>"
"<img src= %# height='30' width='30' />"
, [[play quotations] objectForKey:#"Heading"], myFilePath, .......
SO now what should i do so that it picks the path of image...in html from which it is loaded in web view...
pls help
Thanks
With your approach that is critically important to give the correct baseUrl to load the webView.
If you need the base url to be not local to load some external data, consider displaying images via base64 presentation, i gave the details how you can do it in this answer.
At the OP of this thread you will also see how the base url must be used if you want to display the local content only.

display local html file with css in iphone..?

I want to display local html file that has css too in UIWebView. I am able to do that using following code..
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
NSString *path = [[NSBundle mainBundle] pathForResource:#"info" ofType:#"html"];
NSString *htmlString = [NSString stringWithContentsOfFile:path];
[webView loadHTMLString:htmlString baseURL:baseUrl];
I am not able to get hover effects and my understanding is my be there is no hover kind of thing in a touch mobile,
Any ways my main issue is I had deleted my index.html from resources and then re added the updated one but not getting the updated one..
I don't know what happen the web view still loads the old html file. I tried to clean project too.
Second thing can I get css effects in iphone..?
Thanks..
As far as I know you can only achieve this result by using javascript-events like ontouchstart (or something).
I believe if you reset your simulator (While running your simulator, go to iOS Simulator > Reset Content and Settings in the menu bar) then re-run your app, the UIWebView will load the new index.html. UIWebViews cache these things, so you can add code to clear that cache and/or ignore the cache. Check here: Clearing UIWebview cache

iPhone SDK: Loading resources from custom URL scheme

I have HTML string which is created using an xml file by a third party library. The HTML string contains custom URLs for images and videos (Ex:image://). Is there is way by I can handle these resource load request and load them correctly in UIWebView?
You should be able to change them to a file:// url that points to a file inside the application bundle.
To get the path you can use:
NSString *path = [[NSBundle mainBundle] pathForResource:#"MyFileInResources" ofType:#".png"];
Note that you will need to escape this using:
NSString *escapedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
We need to create a subclass of NSURLProtocol and re-implement the following methods that can handle the custom URL scheme
+(BOOL)canInitWithRequest:(NSURLRequest*)request
+(NSURLRequest*)canonicalRequestForRequest:(NSURLRequest*)request
+(BOOL)requestIsCacheEquivalent:(NSURLRequest*)a toRequest:(NSURLRequest*)b
-(void)startLoading
-(void)stopLoading
We also have to register this custom URL protocol class when the app launches in the following way
[NSURLProtocol registerClass:[CustomURLProtocol class]];

Is that possible to cache UIWebView in iPhone?

I was using NYTimes iPhone application, I become bit curious when I notice that it cache UIwebview even though I didn't open that article.Does anyone have idea how to do that?
How NYTimes iPhone application doing offline reading?
Any help greatly appreciated.
Thanks,
Amit
I wrote the NYTimes app. Here are some details that you could have gotten by looking inside the app bundle.
I download the HTML for the articles, strip out whatever unsupported HTML and JS crud the producers stuffed in it and cache it in the backing store.
The article content is contained in a series of P tags (HTML fragment). I stuff that into a special HTML skeleton page that ships with the app. The static wrapper page also contains CSS and JS used to properly display the article and lazily load the images.
The image loading is really cool. The web view is notified when the images are ready. layout is not affected because I already know the sizes of the missing images.
You can use UIWebView to load html files stored locally on the iPhone.
NYTimes app is probably caching html and images in local storage.
Search google for "UIWebView local" and you get several useful hits.
I tried it out and it works great:
First, create a "view based application" and add a UIWebView to the NIB.
Second, add this code to your UIViewController code:
- (void)viewDidLoad
{
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[super viewDidLoad];
}
Third, add a file called "index.html" to your "Resources" folder in xcode and it will be displayed.
UPDATE:
Indeed, the complicated part of this is downloading the images and stylesheets for the webpage. Doing this server side is easy with Simple HTML Parser (and PHP). Just package everything in a zip and download to your iPhone.
Alternatively, you could do it locally with a C/C++/OBJC HTML parser (libxml2.2 is available on iOS). See this SO question Parsing HTML on the iPhone.
It's going to a bit of a project, so good luck.