2015-11-23 83 views
0

你好,我只是創建一個樣本項目,我有登錄和註冊class.i創建一個註冊類與所有textfields和註冊成功與coredata.The問題是我需要註冊用戶登錄類,使其成功進入下一頁。我必須檢查用戶是否存在或不在我的Login類中。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; 
    [email protected]([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 
+0

您希望用戶在登錄屏幕註冊後登錄嗎? – channi

+0

是的正確@ channi ..... – Arun

回答

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]; 

} 

我想這是非常強的自我解釋的。希望這可以幫助你理解。

+0

但我需要獲得警報或移動到下一個屏幕,如果他們存在..如何在登錄操作中調用此操作?@Saheb Roy – Arun

+0

我是否需要爲註冊類中的鍵設置值太? – Arun

+0

返回值YES或NO會告訴您需要轉到下一個屏幕,相應地進行編輯。 –