0

Alright, I am still messing around with Core Data in my iOS project, and I think I have a good idea how it works conceptually, but am struggling how I should implement into my project. I created a scene in my Xcode project where I want the user / operator of the software to create a user account, have it stored in the project.

First off let me state I started the project as a single view application and I implemented Core Data into my project by doing the following:

  • linking to the Core Data framework in my project
  • then created another project with Core Data checkbox checked
  • copied / pasted the code in the AppDelegate.h/m files.
  • created file.xcdatamodeld
  • added the following line to the KegCop-Prefix.pch file #import <CoreData/CoreData.h>
  • created an Entity Account, and added the following attributes email phoneNumber username in the file.xcdatamodel

After completing what I found to be the necessary steps, I sought out to find some tutorials on core data. I did manage to find some, but they all seemed to deal with using a UITableViewController The view controller that I am trying to implement Core Data in my project is a UIViewController

Then after learning that one shouldn't store passwords in a Core Data database I decided to figure out how I was going to store the pin that the user would create. I came across a tutorial that implements Core Data & Keychain's to store nonsensitive data in the Core Data database and sensitive data in the Keychain. The tutorial can be found here. The tutorial however wasn't ARC ready so some modification was needed to some of the KeychainHelper.m file. After some help from stackoverflow, the KeychainHelper.m file appears to be ARC ready.

Now after completing the above stated tutorial I had three new classes in my project, AccountBase, Account, and KeychainHelper. My project is building without any errors at the moment \o/ but I want to be able to implement the newly created classes into my project, i.e. actually use them. This is something the tutorial doesn't discuss. Now keep in mind I've been using Xcode almost on a day to day basis for almost a month now so I am still new to a lot of things. And one of those things is how I would implement these new classes into my project.

Basically I want the user to type in a username, pin (twice) email, and a phone number. I want to store the username, email, and phone number in the core data database, and store the pin in the keychain.

How would I use the newly created class file Account in my ViewControllerCreate to retrieve the values the user inputs into the text fields and store them to the Core Data database?

Sorry for such a long post, thought I would just try and make this clear as possible so there wouldn't be any confusion.

0

1 Answer 1

3

I was able to solve this problem by importing the Account class into the header of the ViewControllerCreate class with the following line of code.

#import "Account.h"

I was able to use the ManagedObjectContext through out the various classes / view controllers with the following code

// Core Data

if (_managedObjectContext == nil)
{
    _managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
    NSLog(@"After _managedObjectContext: %@", _managedObjectContext);
}

I was then able to utilize the Account class in the ViewControllerCreate with the following code:

// Core Data - retrieve values from text fields and store in database.
    Account *newAccount;
    newAccount = [NSEntityDescription insertNewObjectForEntityForName:@"Account" inManagedObjectContext:_managedObjectContext];
    [newAccount setValue:_createUserTextField.text forKey:@"username"];
    [newAccount setValue:_createEmailTextField.text forKey:@"email"];
    [newAccount setValue:_createPhoneNumber.text forKey:@"phoneNumber"];

    // TODO store pin in keychain
    [newAccount setPassword:_createPinTextField.text];
    NSLog(@"Pin saved is %@", [newAccount password]);
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.