Quote:
Originally Posted by Bertrand21
|
I looked at that video, and this is what I came up with. What I am ultimately trying to do is save webpages for offline viewing. So, I subclassed UIWebView and put in this.
Code:
- (id)initWithCoder:(NSCoder *)decoder {
self = [[decoder decodeObjectForKey:@"webView"] retain];
return self;
}
- (void)encodeWithCoder:(NSCoder *)encoder {
[encoder encodeObject:self forKey:@"webView"];
}
Then, when I want to write to NSUserDefaults, I have this.
Code:
CustomWebView *cachedPage = webView;
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:cachedPage];
NSMutableArray *cacheArray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"pageArray"]];
[cacheArray addObject:data];
NSLog(@"there are %i cached pages", [cacheArray count]);
[[NSUserDefaults standardUserDefaults] setObject:cacheArray forKey:@"pageArray"];
[cacheArray release];
NSLog(@"succesfully cached page");
Then, when I want to use the cached web view, I have this.
Code:
int viewToLoad = [[NSUserDefaults standardUserDefaults] integerForKey:@"cachedViewToLoad"];
NSMutableArray *tempArray = [[NSMutableArray alloc] initWithArray:[[NSUserDefaults standardUserDefaults] objectForKey:@"pageArray"]];
NSLog(@"there are %i cachedViews", [tempArray count]);
NSLog(@"showing view %i", viewToLoad);
CustomWebView *offlineWebView = [NSKeyedUnarchiver unarchiveObjectWithData:[tempArray objectAtIndex:viewToLoad]];
offlineWebView.frame = CGRectMake(0, self.navigationController.navigationBar.frame.size.height,
self.view.frame.size.width,
self.view.frame.size.height - self.navigationController.navigationBar.frame.size.height);
[self.view addSubview:offlineWebView];
[offlineWebView release];
[tempArray release];
I don't get an error when I write the array to NSUserDefaults, so I think the webviews are in there. However, I do get an error when I try to use them.
Thanks!