display local html file with css in iphone..? - 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

Related

Display HTML file in uiwebview from SQLite database?

I have a project that I am working on that requires me to be able to display an HTML file stored in a SQLite database in a uiwebview. I know how to display a local file stored in the main bundle, but I just can't figure out how to get these files stored in the SQLite database to display in the uiwebview. If anyone can help me it would be greatly appreciated. Sample project or tutorial is great! Thanks in advance!
ADDED INFO
I thought I would attach this link to help in understanding what I'm trying to do. http://www.google.com/url?sa=t&source=web&cd=1&ved=0CCYQFjAA&url=http%3A%2F%2Fdblog.com.au%2Fiphone-development-tutorials%2Fiphone-sdk-tutorial-reading-data-from-a-sqlite-database%2F&rct=j&q=sqlite%20animals%20tutorial%20iphone&ei=41eIT-79L9H-2QW50tXaCQ&usg=AFQjCNGqongc81oHHa3WGFTctkBIiduCvA in the linked tutorial I have dropped the image view since I don't need it for my purposes, and I have replaced the data in the database to be the contents of an html file. In the tutorial it displays the text in a textview and I want to change it to display in a uiwebview. I want this to keep formatting and a few other reasons. Thanks to those who have posted. If anyone can help me get this it would be great!! Thanks in advance!
webView loadHTMLString:baseURL: (I assume it is just stored as a string....if you have any images, you will need to set your base directory accordingly)
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"]isDirectory:NO]]];
this code displays "test.html" which is located in the project folder
Got it by using
NSString *html = [animal description];
[self.animalView.animalDesciption loadHTMLString:html baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
when the ViewController that contains the webview is pushed into the display.

background image in html page in iphone

I m the beginner in iphone development.
How i call the background image from my project directory in Xcode in html page. I have taken previously UISegment control and after that i calling two html page on webview in uisegmentcontroller.
Now i want to set background images on those html pages.
It is possible?
if it is yes then how?
thanks in advance..
You can specify a base URL when loading HTML in a UIWebView. If you set the base URL to your bundle path, you can simply reference the background image as if it was in the current directory.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlPage baseURL:baseURL];
in your HTML:
body {
background: url(bg.jpg); // "bg.jpg" is in your bundle
}

UIWebView locally save images and just load html

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

UIWebView display locally stored website (HTML, images, Javascript)

I've looked EVERYWHERE for this, can't find anything. I basically need to store an entire website inside of my iPhone for some interesting reasons. Regardless I can't figure out how to display it correctly in my UIWebView.
EDIT: I should clarify, I can load the original HTML file, and I have chagned all of the pathing to be local, except nothing gets linked in.
Here is the code
self.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *path = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self loadRequest:request];
index.html has a bunch of <script type="text/javascript" src="somescript.js">
None of the JS code gets executed
Looks like you're loading the HTML from inside your bundle. This means that all the additional files (.js, .css, and any media files) also need to be present in your bundle. So the first thing to check is to look inside the contents of your executable and make sure the js, etc. files are included.
If that looks fine the next thing to check is if the html, js, or css files reference content via relative or absolute URLs. If there's an absolute path reference in the web content then UIWebView is going to try to download that content each time so it'll only work when you have a net connection. If the path is relative then it's going to look in the bundle to see if such a file exists.
When you included the html and content into the XCode project file you probably dragged the file(s) over to the project side-bar and were asked whether to "Recursively create groups for any added folders" or to "Create Folder References for any added folders."
The default is the first one which means XCode creates a yellow folder in your project, but it'll ignore the directory hierarchy on disk when time comes to generate the output bundle. If you choose the second option then the folder is blue and if you look in your output bundle you'll see that the whole folder hierarchy has been replicated.
The first works for simple web pages where everything is at the same folder level and you can use the method you list above to load it. The second case works better if your web page is complex and references content in sub-folders in which case you need to load the web pages from a relative path (say, the 'webpages' folder):
NSString *path = [[NSBundle mainBundle]
pathForResource:#"index" ofType:#"html"
inDirectory:#"webpages"];
The last thing to check for is if there are any BASE tags in the html file. This is a way to specify a default address or target for all links on a page, but it can muck up webview links.
The problem is that this call:
NSURL *url = [NSURL fileURLWithPath:path];
doesn't setup a baseURL and so relative paths in the .html file for things like javascript, css, images etc don't work.
Instead use this:
url = [NSURL URLWithString: [path lastPathComponent]
relativeToURL: [NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory: YES]];
and then things like "styles.css" in the index.html file will be found IFF they are copied into the bundle next to the .html file.
You need to set this:
myWebView.dataDetectorTypes = UIDataDetectorTypeLink
Make sure that the .js files are in your copy to resource bundle section and not in the compile section. Xcode places them in the compile group by default.
When adding pathFor resource in Dictionary , it displays a nil string error.
My attempt was to run an entire web page out of the Xcode project file. To do that you must:
When importing the file select "Create folder references for any added folders".
Set up the web view, but make sure you set the relative path as previously mentioned.
NSString *path = [[NSBundle mainBundle] pathForResource:#"filename"
ofType:#"html"
inDirectory:#"Directory"];
NSURL *url = [NSURL URLWithString:[path lastPathComponent] relativeToURL:
[NSURL fileURLWithPath: [path stringByDeletingLastPathComponent]
isDirectory:YES]];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.currentWebView loadRequest:request];

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.