Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 05-02-2009, 07:27 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Default Save Objects

Hi all,

I have an array with multiple objects in it. I want to write it to a file to access it later, and have it saved if the application is closed. I tried to write the array to NSUserDefaults, but apparently, I can't do it because the objects are not integers, strings, or plist files. How can I do this?
brian515 is offline   Reply With Quote
Old 05-02-2009, 10:25 PM   #2 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,890
Default

check out Archivers and unarchivers here
__________________
I really do this.
Bertrand21 is offline   Reply With Quote
Old 05-02-2009, 11:11 PM   #3 (permalink)
Registered Member
 
Join Date: Feb 2009
Posts: 109
Default

Quote:
Originally Posted by Bertrand21 View Post
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!
brian515 is offline   Reply With Quote
Old 05-03-2009, 04:01 PM   #4 (permalink)
Beast Mode
 
Join Date: Dec 2008
Age: 21
Posts: 1,890
Default

can you narrow down the line giving you the error?
__________________
I really do this.
Bertrand21 is offline   Reply With Quote
Reply

Bookmarks

Tags
data, file, nsuserdefaults, objects, save

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 284
17 members and 267 guests
2WeeksToGo, ADY, dacapo, Dani77, Fritzer, ghost, HDshot, headkaze, iDifferent, mer10, mystic.purple, Rudy, smethorst, stoneage, superg, tathaastu, Zool
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,878
Threads: 89,224
Posts: 380,732
Top Poster: BrianSlick (7,129)
Welcome to our newest member, olga2000
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:43 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0