Coredata取概念

时间:2015-11-23 07:13:45

标签: ios objective-c core-data

您好我只是创建了一个示例项目,其中我有登录和注册class.i创建了一个包含所有文本字段的注册类,并使用coredata成功注册了。问题是我需要让注册用户进入登录类才能生成它成功进入下一页。我必须检查用户是否存在于我的登录类中。

Signup.m

@implementation CoreSignup

@synthesize managesObjectContext=_managesObjectContext;
@synthesize managesObjectModel=_managesObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;


#pragma mark Insert Row In CoreData

-(void)insertSignUpList:(NSMutableDictionary *)details_Ary
{
    BOOL isInserted=[self insertRowForSignUp:[details_Ary valueForKey:@"Firstname"] Lastname:[details_Ary valueForKey:@"Lastname"] password:[details_Ary valueForKey:@"Password"] emailid:[details_Ary valueForKey:@"Emailid"] phoneNo:[details_Ary valueForKey:@"Phoneno"] city:[details_Ary valueForKey:@"City"]];

    if (isInserted)
    {
        //inserted Successfully

        NSLog(@"inserted Successfully");
    }
}

-(BOOL)insertRowForSignUp:(NSString *)firstName Lastname:(NSString *)LastName password:(NSString *)password emailid:(NSString *)emailid phoneNo:(NSString *)phoneNo city:(NSString *)city{

    AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];

    SignUp *signUpInfo=[NSEntityDescription insertNewObjectForEntityForName:@"SignUp" inManagedObjectContext:context];
    signUpInfo.firstName=firstName;
    signUpInfo.lastName=LastName;
    signUpInfo.password=password;
    signUpInfo.phoneno=@([phoneNo intValue]);
    signUpInfo.city=city;
    signUpInfo.emailid=emailid;

    NSError *error;
    if (![context save:&error]) {

        NSLog(@"Oops coudnt save");
        return NO;
    }

    return YES;
}

-(NSMutableArray*)fetchAll{

     AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];

    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
    [request setEntity:entity1];

    NSArray *emptyArray=[self.managesObjectContext executeFetchRequest:request error:nil];
    NSMutableArray *AllHistory=[NSMutableArray new];

    for (SignUp*signUpHistory in emptyArray) {

        NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.firstName] forKey:@"firstname"];
       [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.lastName] forKey:@"lastName"];
      [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.password] forKey:@"password"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.emailid] forKey:@"emailid"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.city] forKey:@"city"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.phoneno] forKey:@"phoneno"];
        [AllHistory addObject:tempDict];

    }
    return AllHistory;
}

Login.m

@interface ViewController ()<UITextFieldDelegate>

@end

@implementation ViewController
@synthesize JJuser;
@synthesize JJpassword;
@synthesize managesObjectContext=_managesObjectContext;
@synthesize managesObjectModel=_managesObjectModel;
@synthesize persistentStoreCoordinator=_persistentStoreCoordinator;


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self jjtext];
    [self jjpass];
}

-(void)jjtext{
    JJuser.delegate=self;
    [JJuser enableMaterialPlaceHolder:YES];
}

-(void)jjpass{
    JJpassword.delegate=self;
    [JJpassword enableMaterialPlaceHolder:YES];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSMutableArray*)fetchLogin{

    AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];

    NSFetchRequest *request=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
    [request setEntity:entity1];

    NSArray *emptyArray=[self.managesObjectContext executeFetchRequest:request error:nil];
    NSMutableArray *AllHistory=[NSMutableArray new];

    for (SignUp*signUpHistory in emptyArray) {

        NSMutableDictionary *tempDict=[[NSMutableDictionary alloc]init];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.firstName] forKey:@"firstname"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.lastName] forKey:@"lastName"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.password] forKey:@"password"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.emailid] forKey:@"emailid"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.city] forKey:@"city"];
        [tempDict setObject:[NSString stringWithFormat:@"%@",signUpHistory.phoneno] forKey:@"phoneno"];
        [AllHistory addObject:tempDict];

    }
    return AllHistory;
}



- (IBAction)Login:(id)sender {

    AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

    NSManagedObjectContext *context =
    [appDelegate managedObjectContext];

    NSFetchRequest *FindIt=[[NSFetchRequest alloc]init];
    NSEntityDescription *entity1=[NSEntityDescription entityForName:@"SignUp" inManagedObjectContext:context];
    [FindIt setEntity:entity1];

    NSPredicate *pred = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"firstName = \"%@\"",FindIt]];
    [FindIt setPredicate:pred];

    NSUInteger count = [context countForFetchRequest:FindIt error:nil];
    if (count == NSNotFound)
    {
        UIAlertView*alert=[[UIAlertView alloc]initWithTitle:@"Hello" message:@"No Such id exist" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
        [alert show];

    }else if (count == 1)
    {

    }
    else{


    }

}

- (IBAction)Signup:(id)sender
{
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    SignUpViewController*SignUp = [board instantiateViewControllerWithIdentifier:@"SignUp"];
    [self.navigationController pushViewController:SignUp animated:YES];
}
@end

1 个答案:

答案 0 :(得分:1)

您可以使用获取请求来检查用户是否已经存在,如果存在,则密码是否匹配

试试这个并告诉我它是否有效

-(BOOL)searchDatabaseForUser:(NSString *)username andPassword:(NSString *)password{

AppDelegate *appDelegate =[[UIApplication sharedApplication] delegate];

NSManagedObjectContext *context = [appDelegate managedObjectContext];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"EntityName"];

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"username = %@",username];
[fetchRequest setPredicate:predicate];

NSError *error = nil;


NSArray *result = [context executeFetchRequest:fetchRequest error:&error];

if([result count]){
    for (NSManagedObject *obj in result) {
        if([[obj valueForKey:@"password"]isEqualToString:password]){
            return YES;
        }
    }

    return NO;
    //passwords doesnt match even though username found
}
else {
    return NO;
    // No such Username found
  }
}

现在调用此方法,以确定用户应该在哪里验证用户应该到下一个屏幕,就像这样

Login.m

-(IBAction)someButtonToCheckLogin:(id)sender{

     BOOL shouldUserLogin = [self searchDatabaseForUser:UserNameTextField.txt andPassword:passwordTextField.txt];
if(shouldUserLogin){
//navigate to the next screen.
}
else {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title of the application" message:@"Password mismatch, Please check again" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
 [alert show];

}

我想这很容易解释。希望这有助于您理解。