Three20 display local html file - html

I'm trying to display a local html file from a three20 table controller. The table shows up correctly, but when I select the item, all I get is a blank screen. If I use an external URL, the page shows correctly. I haven't tried using UIWebView, but I was wondering if there was an easy way using the Three20 framework.
Thanks.

EDIT: I forgot that has changed recently...
Assuming you have registered a TTWebController in your navigator that way :
TTNavigator* navigator = [TTNavigator navigator];
TTURLMap* map = navigator.URLMap;
[map from:#"*" toViewController:[TTWebController class]];
You can simply open a local html file that way :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
TTOpenURL([url description]);
Then all you have to to is to add TTTableLinkedItem or subclass (almost all of TT*Cell are) to your dataSource with a local file URL like that :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
TTTableTextItem *item = [TTTableTextItem itemWithText:#"foobar" URL:[url description]];
Much simpler that what I wrote before...
ENDOFEDIT
Forget whats under, unless you're interested in others solutions... (more complex... or not, see last one)
Basically here is how you load content in a UIWebView :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
The three20 TTWebController can take a request in a query :
- (id)initWithNavigatorURL:(NSURL*)URL query:(NSDictionary*)query {
if (self = [self initWithNibName:nil bundle:nil]) {
NSURLRequest* request = [query objectForKey:#"request"];
if (nil != request) {
[self openRequest:request];
} else {
[self openURL:URL];
}
}
return self;
}
So to open a local html file in a TTWebController you could do this :
NSString *creditsPath = [[NSBundle mainBundle] pathForResource:#"credits" ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:creditsPath];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[[TTNavigator navigator] openURLAction:
[[[TTURLAction actionWithURLPath:#"url://to/your/ttwebcontroller"]
applyQuery:[NSDictionary dictionaryWithObject:ulrRequest
forKey:#"request"]]
applyAnimated:YES]];
Now the last part, to trigger this from a TTTableViewController...
You have in your ViewController to implement :
- (void)didSelectObject:(id)object atIndexPath:(NSIndexPath*)indexPath {
// there you retrieve a *TTTableItem* corresponding to your row
// and call previous snippet from URL...
// TTTableItem class has userInfo property you could use:
TTTableItem *item = (TTTableItem *)object;
NSString *htmlFile = item.userInfo.
[self openLocalHtmlFile:htmlFile];
}
Hope that helps!
Nothing tested, at all, should not compile or whatsoever, but that is a solution.
That's a solution using basic three20 + iOS features. You could also write a TTCustomWebController inheriting from TTWebController that would take urls like #"myapp://openLocalHtml?file=credits" that's not that hard to do...
...That here is a draft :
#interface TTCustomWebController : TTWebController {}
#end
#implementation TTCustomWebController
- (id)initWithFile:(NSString *)name {
self = [super init];
if (self) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:name ofType:#"html"];
NSURL *URL = [NSURL fileURLWithPath:filePath];
[self openURL:URL];
}
return self;
}
#end
Then you could simply use TTTableLinkedItem in your datasource pointing to these #"myapp://openLocalHtml?file=credits"...
Good luck! :)

Related

How do you log in to a website from an iOS Webview?

I'm working on an iOS app for my school that will allow the user to access school documents like schedules, calendars, and grade/tuition sites. I've been doing research on how to save credentials for some of the sites that require logins, and my ultimate goal is not only save credentials, but automatically log in the user to the websites when the webview is loaded as well. So far my code looks like this:
#import "PJP Webview.h"
#implementation PJP_Webview
#synthesize webView, loginView, username, password, body, urlToLink,usernameField,passwordField, firstTime, loginNeeded, callerCell;
-(void) viewDidLoad {
[super viewDidLoad];
loginView.hidden = true;
if (firstTime && loginNeeded) {
loginView.hidden = false;
[self.view bringSubviewToFront:loginView];
}
else {
loginView.hidden = true;
[self.view addSubview:self.webView];
[self.view bringSubviewToFront:webView];
if (loginNeeded) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
self.username = [userDefaults objectForKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
self.password = [userDefaults objectForKey:passwordSave];
NSURL *url = [NSURL URLWithString: urlToLink];
NSString *bodyForUse = [NSString stringWithFormat: body, username, password];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [bodyForUse dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
}
else {
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
}
}
- (IBAction)acceptInput:(id)sender {
self.username = usernameField.text;
self.password = passwordField.text;
loginView.hidden = true;
[self.view addSubview:self.webView];
[self.view bringSubviewToFront:webView];
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
[userDefaults setObject:username forKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
[userDefaults setObject:password forKey:passwordSave];
[userDefaults synchronize];
NSURL *url = [NSURL URLWithString: urlToLink];
NSString *bodyForUse = [NSString stringWithFormat: body, username, password];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL: url];
[request setHTTPMethod: #"POST"];
[request setHTTPBody: [bodyForUse dataUsingEncoding: NSUTF8StringEncoding]];
[webView loadRequest: request];
}
#end
I modeled some of my code after another stack overflow article found here Loading a webpage through UIWebView with POST parameters
An example of one of the websites I am trying to integrate is https://adphila.gradeconnect.com/front/slogin.php
So far my code has not been successful at logging me into the website and taking me to the next page, so I'm not sure if maybe the body I am using for the parameters is wrong, if my code is faulty, or some other issue is present. Also, if you have any suggestions for how to make my previous code more efficient I welcome that too!
Edit: The school also uses gmail for student emails, so is it possible to embed a uiwebview of email content as well? I know Google has their own APIs but I have never used non-Apple libraries before.
Edit 2:
#import "PJP Webview.h"
#implementation PJP_Webview
#synthesize webViewCurrent, loginView, username, password, bodyUsername, bodyPassword, urlToLink,usernameField,passwordField, firstTime, loginNeeded, callerCell, bodyButton;
-(void) viewDidLoad {
[super viewDidLoad];
[webViewCurrent setDelegate:self];
loginView.hidden = true;
if (firstTime && loginNeeded) {
loginView.hidden = false;
[self.view bringSubviewToFront:loginView];
}
else {
loginView.hidden = true;
if (loginNeeded) {
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
self.username = [userDefaults objectForKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
self.password = [userDefaults objectForKey:passwordSave];
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
}
else {
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
}
[self.view addSubview:self.webViewCurrent];
[self.view bringSubviewToFront:webViewCurrent];
}
}
- (IBAction)acceptInput:(id)sender {
self.username = usernameField.text;
self.password = passwordField.text;
loginView.hidden = true;
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *usernameSave = [NSString stringWithFormat:#"%#username", self.callerCell];
[userDefaults setObject:username forKey:usernameSave];
NSString *passwordSave = [NSString stringWithFormat:#"%#password", self.callerCell];
[userDefaults setObject:password forKey:passwordSave];
[userDefaults synchronize];
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webViewCurrent loadRequest:request];
[self.view addSubview:self.webViewCurrent];
[self.view bringSubviewToFront:webViewCurrent];
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
NSString *fillData = [NSString stringWithFormat:#"document.getElementById('%#').value = '%#';document.getElementById('%#').value = '%#';", bodyUsername, username, bodyPassword, password];
[webView stringByEvaluatingJavaScriptFromString:fillData];
NSString *buttonData = [NSString stringWithFormat:#"document.getElementById('%#').click()", bodyButton];
[webView stringByEvaluatingJavaScriptFromString:buttonData];
}
#end
There is a new error though, void SendDelegateMessage(NSInvocation *): delegate (webView:didFinishLoadForFrame:) failed to return after waiting 10 seconds. main run loop mode: kCFRunLoopDefaultMode
For the website I am using, the bodyUsername is lusername, the bodyPassword lpassword, and the bodyButton login. The page loads but is not executing the javascript.
You can do this by Autopopulating the username and password field of that webpage. First get the name of the username and password field from the webpage. right click on the webpage.
Click on inspect element and then You van easily find it.
Then write the WebView Delegate Method.
// ViewController.m
// WebDemo
//
// Created by Nilesh on 7/31/16.
// Copyright © 2016 Nilesh. All rights reserved.
//
#import "ViewController.h"
#interface ViewController ()<UIWebViewDelegate>
#property (weak, nonatomic) IBOutlet UIWebView *webView;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *urlToLink = #"https://adphila.gradeconnect.com/front/slogin.php";
NSURL *url = [NSURL URLWithString: urlToLink];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[_webView loadRequest:request];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) webViewDidFinishLoad:(UIWebView *)webView {
NSString* userId = #"userName" ;//Your userName
NSString* password = #"password";// Your Password
NSString* jScriptString1 = [NSString stringWithFormat:#"document.forms[0]['lusername'].value='%#'", userId];
NSString* jScriptString2 = [NSString stringWithFormat:#"document.forms[0]['lpassword'].value='%#'", password];
[webView stringByEvaluatingJavaScriptFromString:jScriptString1];
[webView stringByEvaluatingJavaScriptFromString:jScriptString2];
NSString *jsStat = #"document.forms[0]['login'].click()";
[webView stringByEvaluatingJavaScriptFromString:jsStat];
}
#end

viewDidLoad not being called with UIViewController is pushed

I'm trying to launch a UIViewController when a table line is clicked...
The code is main view controller is ...
- (void)showDetail:(NSString *)detail
{
DetailViewController *secondViewController = [[DetailViewController alloc] initWithNibName:#"DetailViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:secondViewController animated:YES];
[DetailViewController release];
}
I filled in the viewDidLoad method in my "DetailViewController" class that is attached to the NIB.
- (void)viewDidLoad:(BOOL)animated;
{
NSURL *url = [NSURL URLWithString:#"www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}
When I hit the pushViewController method, the NIB appears to load (can see a test label on it) but the viewDidLoad method is not called. In that method, I'm trying to load a test HTML into a UIWebView.
Any ideas?
There is no event viewDidLoad: change to the bellow it should work.
- (void)viewDidLoad{
NSURL *url = [NSURL URLWithString:#"www.google.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
}

When selecting a row in table, it switches to blank screen

in part of my app (storyboard) I have a table view with various rows, and when I select a row, an html file should be displayed. Instead when I select a row, I get a blank screen instead.
I thought I needed another view controller after the tableview controller, but I get the same issue whether I have one or not.
In my table controller .m file I have the following code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
articleIndex = indexPath.row;
WebView *selectedArticle = [[WebView alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:selectedArticle animated:YES];
[selectedArticle release];
That is allowing me to select a row and the screen changes to a blank one, rather than loading the relevant html file.
I have in my resources folder a .h and .m file as follows:
webView.h
#import <UIKit/UIKit.h>
#import "globals.h"
#interface WebView : UIViewController{
NSArray *articleNames;
IBOutlet UIWebView *webView;
}
#property(nonatomic, retain)IBOutlet UIWebView *webView;
#end
webView.m
#import "WebView.h"
#implementation WebView
#synthesize webView;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
articleNames = [[NSArray arrayWithObjects:
#"chapter1",
#"chapter2",
#"chapter3",
#"chapter4",
#"chapter5",
#"chapter6",
nil] retain];
NSString *chapterName = [[NSString alloc] initWithString:[articleNames objectAtIndex:articleIndex]];
NSString *path = [[NSBundle mainBundle] pathForResource:chapterName ofType:#"html"];
NSURL *url = [NSURL fileURLWithPath:path];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[webView loadRequest:urlRequest];
[chapterName release];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#end
I can't see what I have done wrong here, any advice? I'm not sure how to get the html files to display or where I need to make changes to allow it. If I remove the HTML files from the project the app crashes so it's definitely loading them but for some reason not displaying...
Well first off where is "articleIndex" I see you set it in your tableViewController but it is not webView.h or webView.m. I would suggest first adding in a NSLog
NSURL *url = [NSURL fileURLWithPath:path];
NSLog(#"Article URL:%#", url);
This will let you see if the url is correct first. Second it looks like you connected your UIWebView with interface builder, so make sure that the webView is connect correctly. You can also test the webView buy using:
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString: #"http://www.apple.com"]];
And see if apple.com loads. If it does then you know your issue is with your html files or the paths to them.

Using images and css file for a generated HTML UIWebView

I'm generating an HTML file for an app, and in this HTML file there's a link to a stylesheet and one to an image.
Here's what I tried so far:
NSMutableString *toReturn = [NSMutableString stringWithCapacity:100];
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"etapes.css" ofType:nil];
[toReturn appendFormat:#"<html><head><title></title><link href=\"%#\" media=\"all\" rel=\"stylesheet\" type=\"text/css\" /></head><body>", cssPath];
It generates the right full path to the right file, this is okay if I want to access it on my mac, but on the simulator or my iPhone it doesn't point to the right place at all...
Do you have any idea of how could I make this?
Thanks
I found this tutorial a while back. I ended up just using a <style> tag in the end though.
NSString *path = [[NSBundle mainBundle] bundlePath];
NSURL *baseURL = [NSURL fileURLWithPath:path];
[webView loadHTMLString:htmlString baseURL:baseURL];
Another way of doing this is like so:
-(NSString *)urlForFileNamed:(NSString *) name ofType:(NSString *) type {
NSString *filePath = [[NSBundle mainBundle] pathForResource: name ofType: type];
if (!filePath) { NSLog(#"No path found for file %#.%#", name, type); }
return filePath;
}
-(void)viewDidLoad {
[super viewDidLoad];
NSString *path = [[NSBundle mainBundle] bundlePath];
baseUrl = [NSURL fileURLWithPath: path];
[view loadHTMLString: [self html] baseURL: [NSURL fileURLWithPath: path]];
}
-(NSString *)html {
// Obviously the below's just a stub for proper HTML
return [NSString stringWithFormat: #"<img src=\"#\" />", [self urlForFileNamed: #"foo" ofType: #"png"]];
}
You may try to edit the line
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"etapes.css" ofType:nil];
to
NSString *cssPath = [[NSBundle mainBundle] pathForResource:#"etapes" ofType:#"css"];

How to save the content in UIWebView for faster loading on next launch?

I know that there are some caching classes introduced in the iphone sdk recently, and there is also a TTURLRequest from three20's library that allows you to cache a request to a URL. However, because I am loading the web page in UIWebView by calling UIWebView's loadRequest, those techniques are not really applicable.
Any ideas how I can save a web page so that on next app launch, I don't have to fetch from the web again for the full page? The page itself already have some ajax mechanism that updates parts of itself automatically.
There are a bunch of articles about the way the cache of the UIWebView works and the global feeling is that even if some mechanisms seems to work OK under MacOS X, the same approaches may have curious behavior under iPhone.
HOWEVER, I'm doing it by playing with the global cache that is accessed by any NSURLConnection, UIWebView included. And in my case, it works ;).
What you need to understand is the global flow:
YOU -> loadRequest on a UIWebView
This goes into NSURLCache to ask "is there something cached for this request?":
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
From that, here's what I do to handle the cache on the disk, on my side, to speed up the load of a UIWebView:
Subclass the NSURLCache and override the get control over the -(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request selector
Reimplement this selector in such a way that if nothing has been written on the FS for this request (no cache), then do the request on your side and store the content on FS. Otherwise, return what has been previously cached.
Create an instance of your subclass and set it to the system so that it is used by your application
Now the code :
MyCache.h
#interface MyCache : NSURLCache {
}
#end
MyCache.m
#implementation MyCache
-(NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"CACHE REQUEST S%#", request);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray* tokens = [request.URL.relativePath componentsSeparatedByString:#"/"];
if (tokens==nil) {
NSLog(#"ignoring cache for %#", request);
return nil;
}
NSString* pathWithoutRessourceName=#"";
for (int i=0; i<[tokens count]-1; i++) {
pathWithoutRessourceName = [pathWithoutRessourceName stringByAppendingString:[NSString stringWithFormat:#"%#%#", [tokens objectAtIndex:i], #"/"]];
}
NSString* absolutePath = [NSString stringWithFormat:#"%#%#", documentsDirectory, pathWithoutRessourceName];
NSString* absolutePathWithRessourceName = [NSString stringWithFormat:#"%#%#", documentsDirectory, request.URL.relativePath];
NSString* ressourceName = [absolutePathWithRessourceName stringByReplacingOccurrencesOfString:absolutePath withString:#""];
NSCachedURLResponse* cacheResponse = nil;
//we're only caching .png, .js, .cgz, .jgz
if (
[ressourceName rangeOfString:#".png"].location!=NSNotFound ||
[ressourceName rangeOfString:#".js"].location!=NSNotFound ||
[ressourceName rangeOfString:#".cgz"].location!=NSNotFound ||
[ressourceName rangeOfString:#".jgz"].location!=NSNotFound) {
NSString* storagePath = [NSString stringWithFormat:#"%#/myCache%#", documentsDirectory, request.URL.relativePath];
//this ressource is candidate for cache.
NSData* content;
NSError* error = nil;
//is it already cached ?
if ([[NSFileManager defaultManager] fileExistsAtPath:storagePath]) {
//NSLog(#"CACHE FOUND for %#", request.URL.relativePath);
content = [[NSData dataWithContentsOfFile:storagePath] retain];
NSURLResponse* response = [[NSURLResponse alloc] initWithURL:request.URL MIMEType:#"" expectedContentLength:[content length] textEncodingName:nil];
cacheResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:content];
} else {
//trick here : if no cache, populate it asynchronously and return nil
[NSThread detachNewThreadSelector:#selector(populateCacheFor:) toTarget:self withObject:request];
}
} else {
NSLog(#"ignoring cache for %#", request);
}
return cacheResponse;
}
-(void)populateCacheFor:(NSURLRequest*)request {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//NSLog(#"PATH S%#", paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray* tokens = [request.URL.relativePath componentsSeparatedByString:#"/"];
NSString* pathWithoutRessourceName=#"";
for (int i=0; i<[tokens count]-1; i++) {
pathWithoutRessourceName = [pathWithoutRessourceName stringByAppendingString:[NSString stringWithFormat:#"%#%#", [tokens objectAtIndex:i], #"/"]];
}
NSString* absolutePath = [NSString stringWithFormat:#"%#/myCache%#", documentsDirectory, pathWithoutRessourceName];
//NSString* absolutePathWithRessourceName = [NSString stringWithFormat:#"%#%#", documentsDirectory, request.URL.relativePath];
//NSString* ressourceName = [absolutePathWithRessourceName stringByReplacingOccurrencesOfString:absolutePath withString:#""];
NSString* storagePath = [NSString stringWithFormat:#"%#/myCache%#", documentsDirectory, request.URL.relativePath];
NSData* content;
NSError* error = nil;
NSCachedURLResponse* cacheResponse = nil;
NSLog(#"NO CACHE FOUND for %#", request.URL);
//NSLog(#"retrieving content (timeout=%f) for %# ...", [request timeoutInterval], request.URL);
content = [NSData dataWithContentsOfURL:request.URL options:1 error:&error];
//NSLog(#"content retrieved for %# / error:%#", request.URL, error);
if (error!=nil) {
NSLog(#"ERROR %# info:%#", error, error.userInfo);
NSLog(#"Cache not populated for %#", request.URL);
} else {
NSURLResponse* response = [[NSURLResponse alloc] initWithURL:request.URL MIMEType:#"" expectedContentLength:[content length] textEncodingName:nil];
cacheResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:content];
//the store is invoked automatically.
[[NSFileManager defaultManager] createDirectoryAtPath:absolutePath withIntermediateDirectories:YES attributes:nil error:&error];
BOOL ok;// = [[NSFileManager defaultManager] createDirectoryAtPath:absolutePath withIntermediateDirectories:YES attributes:nil error:&error];
ok = [content writeToFile:storagePath atomically:YES];
NSLog(#"Caching %# : %#", storagePath , ok?#"OK":#"KO");
}
[pool release];
}
#end
And the use of it in your application:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* diskCachePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"myCache"];
NSError* error;
[[NSFileManager defaultManager] createDirectoryAtPath:diskCachePath withIntermediateDirectories:YES attributes:nil error:&error];
MyCache* cacheMngr = [[MyCache alloc] initWithMemoryCapacity:10000 diskCapacity:100000000 diskPath:diskCachePath];
[NSURLCache setSharedURLCache:cacheMngr];
This code deserves a lot of cleanup.. but the main things should be in there. I had a lot of trouble to get this working, hope this helps.
I recently found this project under github :
http://github.com/rs/SDURLCache
The approach is quite the same as my previous answer described here How to save the content in UIWebView for faster loading on next launch? , but the code looks more polished so maybe it makes sense to give it a try.
If the page has AJAX already, why not store the JavaScript/HTML in the application bundle to start rather than downloading it on the first launch? Then load the page with the code Corey gave below and let the AJAX handle hitting the network for the updated parts of the page.
Take a look at: http://allseeing-i.com/ASIHTTPRequest/ASIWebPageRequest
You can save an HTML in the documents directory and load the page directly from the documents directory on launch.
To save the webview content:
Reading HTML content from a UIWebView
To load:
NSString* path = [[NSBundle mainBundle] pathForResource:#"about" ofType:#"html"];
NSURL* url = [NSURL fileURLWithPath:path];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];