NSURLCache don't support file://? - ios

I would like to load [NSURL fileURLWithPath:#"2.html"] in UIWebView use NSURLCache, the cachedResponseForRequest: function is executed and return a NSCachedURLResponse object which is performed correctly at http:// scheme.
But The UIWebView load failed finally. Did I miss some optional?
I want to use UIWebView load html file in a compressed file, and I do not expect to uncompress the files to disk. is there any suggestion.

Do this:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"2" withExtension:#"html"];
NSString *html = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
NSURL *baseUrl = [NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]];
[webView loadHTMLString:html baseURL:baseUrl];

Related

Show downloaded PDF file with webView ios

I'm making an iPhone app, that will download a PDF file and display it in a webView.
However my script will not show the downloaded PDF. It does download it and save it in Documents, but the webView will not show it.
Here's my script:
NSString *path = [[NSBundle mainBundle] pathForResource:#"3" ofType:#"pdf"];
NSURL *urlen = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
From the official documentation on NSURL official documentation on NSURL.
Sending nil as the argument to fileURLWithPath: produces an exception.
The problem then is actually with [[NSBundle mainBundle] pathForResource:ofType:]. This is returning nil, rather than an actual path to a file.
The problem here is actually that [NSBundle mainBundle] refers to files that are bundle with your app. You need to look in your app's document directory, which is where it stores files it has downloaded.
This method will give you the path to your app's document directory:
NSString* documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
Now, just append the file name to this path:
NSString *pdfPath = [documentsPath stringByAppendingPathComponent:#"3.pdf"];
And for good measure (because crashes are always bad), make sure the file exists as such:
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:pdfPath];
And finish as such:
if (fileExists) {
NSURL *urlen = [NSURL fileURLWithPath:pdfPath];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:urlen];
[webView loadRequest:urlRequest];
[webView setScalesPageToFit:YES];
} else {
// probably let the user know there's some sort of problem
}

CSS and JS Files are not loading in uiwebview

In my app, a website included in a folder. I tried to load it in webview using the following code:
self.wView.dataDetectorTypes = UIDataDetectorTypeLink;
NSString *htmlFile = [[NSBundle mainBundle] pathForResource:#"index" ofType:#"html" inDirectory:NO];
NSLog(#"%#",htmlFile);
//NSData *pathData = [NSData dataWithContentsOfFile:htmlFile];
//NSString *htmlString = [NSString stringWithContentsOfFile:htmlFile encoding:NSUTF8StringEncoding error:nil];
NSURL *url = [NSURL URLWithString:[htmlFile lastPathComponent] relativeToURL:[NSURL fileURLWithPath:[htmlFile stringByDeletingLastPathComponent] isDirectory:YES]];
//(void)[NSURLConnection connectionWithRequest:req delegate:self];
[self.wView loadRequest:[NSURLRequest requestWithURL:url]] ;
I get the file path in nslog.
But in my webview, css and js files are not loading.
Please help me.
Simple Solution for this problem
Just select the option Create Folder references for any added folders while you are copying the Website folder into XCode

UIWebView not loading local webpage

I have a super simple webpage that I just want to load a single local file. The file is /Resources/About.html
Here is my code:
NSString *urlPath = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
NSURL *url = [NSURL fileURLWithPath:urlPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
What's wrong?
The method URLForResource:withExtension returns an NSURL, not an NSString. You can use the result of that directly in your request.
NSURL *URL = [[NSBundle mainBundle] URLForResource:#"About" withExtension:#"html"];
[webView loadRequest:[NSURLRequest requestWithURL:URL]];
NSString * urlPath = [[NSBundle mainBundle] pathForResource:#"About" ofType:#"html"];
NSString *content = [NSString urlPath encoding:NSUTF8StringEncoding error:nil];
[webView content baseURL:[NSURL urlPath]];
I bet URLForResource is returning nil (Can you confirm that it is not?). It will not perform a recursive search, and will fail if your "Resources" folder is an actual folder (blue in XCode) as opposed to a group (yellow in Xcode). Try using the main bundle's resourceURL and appending your path to the end of it.

loading local html5 in uiwebview with anchor

I have the following code to load a local html file in an iOS app.
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
The above works, however I need to load an anchor as this is a single page web app embedded in a UIWebview.
Basically how do I load index.html#settings for instance instead of just index.html.
This worked for me.
NSString *basePath = [[NSBundle mainBundle] pathForResource:#"page.html" ofType:nil];
NSURL *baseURL = [NSURL fileURLWithPath:basePath];
NSURL *fullURL = [NSURL URLWithString:#"#anchor1" relativeToURL:baseURL];
// Now do whatever with this fullURL
The # is converted to %23 in [NSURL fileURLWithPath:basePath], but URLWithString:(NSString *)URLString relativeToURL:(NSURL *)baseURL; not.

UIWebView is crashing when trying to load a local html file

The code I am using in my view controller viewDidLoad method:
NSString *path = [[NSBundle mainBundle] pathForResource:#"test" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [[NSURLRequest requestWithURL:url] autorelease];
[webView loadRequest:request];
I get EXE_BAD_ACCESS
requestWithURL: will give you an autoreleased object so you don't need to autorelease it again.

Resources