Adding folders within iOS app

71 views Asked by At

I am pretty new to the world of iOS development and I would appreciate your help.

Currently in my project, I want the user to:

  • create a new folder
  • name the folder
  • add contacts from the user's contacts
  • save them to this folder

How can I create folders within the app and have these folders hold contact information? In addition, the reason I want to be able to do this is to have the user sort their contacts into folders. If there is a better approach I welcome all suggestions.

2

There are 2 answers

0
Duncan C On BEST ANSWER

When I first read your post's subject I thought you meant file system folders (Directories). You can use NSFileManager calls to do that, within the limits of the iOS sandbox.

However, it sounds like you are taking about application-level logical folders.

You need to read up on the address book APIs. I haven't used them before, so I don't have any specific help for you. I do know that the address book is Core Foundation, which is C level APIs, and harder to use then NSObject-based classes.

Do you have experience in C or another programming language? If you are both new to iOS and to programming, then you are in way, way over your head and need to tackle some simpler projects first.

You will need to come up with a design that lets you represent logical folders-full of contacts. You could probably use nested NSArrays, or better yet, use Core Data to save your contact information.

Explaining how to do either of those things is beyond the scope of a forum post.

0
Jean-Étienne On

You can create folders with the following command:

NSString *path = ...
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:path 
                          withIntermediateDirectories:YES 
                                           attributes:nil 
                                                error:&error];

And you should probably set the path to be somewhere under the "Documents" directory, that you can find like this:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directoryPath = [paths objectAtIndex:0];

However, creating an actual folder structure on disk may not be the simplest or most useful solution to your problem. If you just want your app to let user organise their contacts, then perhaps storing this in a local database (CoreData) may be enough. I can see a very simple table storing the Address Book record ID (ABRecordID) so you can retrieve the contact's data on the fly, along with a folder (ID or name) of your creation. As you develop you app, updating your database schema seems more flexible than writing a lot of file system level code. Dealing with files is always cumbersome, slightly resource intensive and error prone (duplicates, wrong naming, etc.).