Am am making a task organizer iOS app. You type a task and it is saved in an array. I wanted people to be able to share the tasks between Phones so i added a way to save each Array.
Right now I am using my idea locally. The page has a title and a password. When the save button is pressed the array is saved to a file (This works very well and it saves every time) that is unique to the Title and Password.
I need to find a way to then get all the information in the file back to the array so it can be seen. This is what i have tried and keep in mind that everything works fine except for the "get tasks button" my problem is in the getFile
void:
BNRAppDelegate.m
#import "BNRAppDelegate.h"
NSString *docPath()
{
NSArray *pathList = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask,
YES);
return [[pathList objectAtIndex:0] stringByAppendingPathComponent:@"data.td" ];
}
@implementation BNRAppDelegate
@synthesize window = _window;
#pragma mark - Application delegate callbacks
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
NSArray *plist = [NSArray arrayWithContentsOfFile:docPath()];
if (plist)
{
tasks = [plist mutableCopy];
}
else
{
tasks = [[NSMutableArray alloc] init];
}
CGRect windowFrame = [[UIScreen mainScreen] bounds];
UIWindow *theWindow = [[UIWindow alloc] initWithFrame:windowFrame];
[self setWindow:theWindow];
[[self window] addSubview:taskTable];
[[self window] addSubview:taskField];
[[self window] addSubview:titleField];
[[self window] addSubview:insertButton];
[[self window] addSubview:clearButton];
[[self window] addSubview:shareButton];
[[self window] addSubview:passField];
[[self window] addSubview:getButton];
[[self window] setBackgroundColor:[UIColor whiteColor]];
[[self window] makeKeyAndVisible];
return YES;
}
- (void)addTask:(id)sender
{
NSString *t = [taskField text];
if ([t isEqualToString:@""]) {
return;
}
[tasks addObject:t];
[taskTable reloadData];
[taskField setText:@""];
[taskField resignFirstResponder];
}
- (void)takeTask:(id)sender
{
[tasks removeAllObjects];
[taskTable reloadData];
[tasks writeToFile:docPath()
atomically:YES];
}
- (void)saveTask:(id)sender;
{
if ([titleField text] == @""){
//
} else {
NSString * original = [titleField text];
NSString * pass = [passField text];
NSString * step = [NSString stringWithFormat:@"%@.%@", original, pass];
NSString * file = [NSString stringWithFormat:@"%@.plist", step];
[tasks writeToFile:[NSString stringWithFormat:@"/tmp/%@", file]
atomically:YES];
[tasks writeToFile:docPath()
atomically:YES];
}
}
- (void)getFile:(id)sender;
{
NSString * original = [titleField text];
NSString * pass = [passField text];
NSString * step = [NSString stringWithFormat:@"%@.%@", original, pass];
NSString * file = [NSString stringWithFormat:@"%@.plist", step];
NSMutableArray *theTasks = [NSMutableArray arrayWithContentsOfFile:[NSString stringWithFormat:@"/tmp/%@", file]];
tasks = [theTasks mutableCopy];
[tasks writeToFile:docPath()
atomically:YES];
}
- (void)tableView:(UITableView *)tableView commitEditingStyle: (UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[tasks removeObjectAtIndex:indexPath.row]; [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
}
#pragma mark - Table View management
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [tasks count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *c= [taskTable dequeueReusableCellWithIdentifier:@"Cell"];
if (!c) {
c = [[ UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
}
NSString *item = [tasks objectAtIndex:[indexPath row]];
[[c textLabel] setText:item];
return c;
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[tasks writeToFile:docPath()
atomically:YES];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
[tasks writeToFile:docPath()
atomically:YES];
}
@end
Please Help if you can Thank You.