iPhone - How to preload a local file in a UIWebView? - iphone

Is it possible to preload content of a local file with embedded images for a UIWebView?
This is how I'm loading the data inside the UIWebView:
NSString *urlString = [[NSBundle mainBundle] pathForResource:#"info" ofType:#"html"];
NSString *htmlContent = [NSString stringWithContentsOfFile:urlString encoding:NSUTF8StringEncoding error:nil];
[myWebView loadHTMLString:htmlContent baseURL:nil];
But the UIWebView still needs some time to display the content. Is there any way to put this content into the browser cache on app start?

If you create the browser object on app start and load the HTML string immediately, it should work fine to display the view later.
Another option would be to allocate it when necessary, but not actually show the view until it's done loading (wait for - (void)webViewDidFinishLoad:(UIWebView *)webView to be called on the web view's delegate before showing it).
Also, would it make more sense to call
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:urlString]]];
than using the loadHTMLString: method? That way, you don't have to load the HTML string into memory yourself and the web view can load it directly from disk.

Related

Why my page downloaded to folder Documents of iPhone simulator is not loadable?

I made an hybrid app between objective C and javascript. My app works so:
When I press on the first button it will load a UIWebView
In this UIWebView I can save the html to the Documents folder
When I save the website I create a JSON to store informations and I save this JSON to Documents folder
When I press the second button, it will load another UIWebView in which there are a mobile site that read informations from JSON and present this data in a html list
When I press on the name of one site it will load the site from Documents folder
The first 4 points works great, but when I press on the site title I've trouble, indeed, the UIWebView shows me a page in which there are written that the page it's not stored on this server. When I navigate with Finder to the Documents folder of my app and I double click on the html site it shows me it in Safari. Why when I load it with an UIWebView doesn't work? I will post here a screenshot to understand my flow.
CODE: I post here the code to load the html:
viewDidLoad method where I call the method to load my page
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.webView.delegate = self;
//[self loadWebsite:#"http://localhost/root/main/index.html"];
[self loadWebsite:#"http://192.168.2.1/root/main/index.html"];
}
loadWebsite method
-(void)loadWebsite:(NSString*)site
{
NSURL *url = [NSURL URLWithString:site];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView setScalesPageToFit:YES];
[self.webView loadRequest:request];
}
This seems like a problem with how you are loading your html file into the webview. You should post the code where you are loading the uiwebview.
To load a webview with a local file use the following code :
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:#"htmlfilename" ofType:#"html" inDirectory:#"Documents"]];
[webview loadRequest:[NSURLRequest requestWithURL:url]];
You can refer to this post : Load resources from relative path using local html in uiwebview

how to scale to fit pages opened from HTML file containing web links

I have an app I am working on in Xcode. Part of the app area loads an html file. The HTML file itself has some links to external websites. They open fine, except they are not scaled to fit on the iPhone.
How would I go about ensuring all web pages opened are scaled to fit? The code from the .m file is below
NSString *fileString = [[NSBundle mainBundle] pathForResource: #"chapter5" ofType: #"html"];
NSURL *newURL = [[NSURL alloc] initFileURLWithPath: fileString];
NSURLRequest *newURLRequest = [[NSURLRequest alloc] initWithURL: newURL];
[webview loadRequest: newURLRequest];
The HTML file "chapter5.html contains links to websites.
I thought I would try entering
webview.scalesPageToFit = YES
in the .m file hoping it would apply to anything opened, but that didn't work.
Any advice would be greatly appreciated.
Thanks
The scalesPageToFit will take effect only when the webView loads the request. Implement the delegate of UIWebView webViewDidFinishLoad: method and try this code.
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize defaultSize = webView.frame.size;
CGSize fittingSize = [tableViewCell.webView sizeThatFits:defaultSize];
[webView sizeThatFits:fittingSize];
NSLog(#"Size : %#",NSStringFromCGSize(fittingSize));
}
Please set webview mode as Aspect-Fit
Thanks guys, I set webview mode as Aspect-Fit and set the Scaling to scale pages to fit and this worked, not sure how I missed that.

How to load large size PDF scroll view in iOS?

I have pdf of size 53MB when i try to load only one page of PDF in scrollview i got memory warning and then application crash.
Display the PDF in a UIWebView:
NSString *PDFPath = [[NSBundle mainBundle] pathForResource:#"filename" ofType:#"pdf"];
NSURL *URL = [NSURL fileURLWithPath:PDFPath];
NSURLRequest *URLRequest = [NSURLRequest requestWithURL:URL];
[webView loadRequest:URLRequest];
The WebView will load pages into memory as they appear on screen rather than all at once.
This is a duplicate of What is the best way to view large PDFs on iOS?
The accepted answer is to use a UIWebView as indicated above.
You should take a look at the vfr/Reader, which is a PDF Reader Core for iOS.
UIWebView is not very good in handling big files out of the box.

display html embedded data in iphone

I'm wondering whether there's a control to display html embedded data in iphone. I need to display the information as it is on the web. The data is saved into it database together with its html tag.
So is there anyway for me to display the data accordingly..
You can use a UIWebView and load HTML using
- (void)loadHTMLString:(NSString *)string baseURL:(NSURL *)baseURL
you can use a UIWebView and loadHTMLString method to load the data in HTML.
You can also change html page contents at run time using the javascript.
You can use webview for displaying displaying html embbed data.
Example is given here :
To load remote html file:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:#"http://www.apple.com"]]];
To load local html file:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
To load html string:
[webView loadHTMLString:#"<img src=test.png>" baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];

How to check the URL of a WebView?

I have a Subview that load a Webview. In this Webview I load a file HTML locally in the Documents folder of my App. I need to check if the webview load another HTML file. Specifically:
Webview load "index.html" locally in my Documents folder
After 7 days a Javascript load another page with location.href= "index2.html",
When I load this WebView I need to check if the current URL is index.html or index2.html, if is the second I must change Subview!
How can I do this?
NSString *currentUrl = [[[yourWebView request] URL] absoluteString];
Use this NSURL method [NSURL fileURLWithPath:#"some/path/to/your/file" isDirectory:NO] and pass that to a NSURLRequest which is then passed to your UIWebView. You can get the path to your resource by using the [[NSBundle mainBundle] pathForResource...] methods.
NSURL *url = myWebView.request.URL;