2012-06-25 31 views
3

我正在創建一個多用戶iPhone應用程序,並且正在嘗試完成用戶登錄過程中的編碼。我可以成功創建一個帳戶,並將用戶輸入的數據存儲到核心數據數據庫中,並將引腳(密碼)存儲到密鑰鏈中,所以現在我正在嘗試完成登錄過程。下面列出的代碼是我到目前爲止,我想知道我需要做什麼來完成登錄過程。iOS - 如何使用鑰匙串/核心數據創建登錄過程?

- (IBAction)processLogin:(id)sender { 

// hide keyboard 
[_textFieldUsername resignFirstResponder]; 
[_textFieldPin resignFirstResponder]; 


// First - make activity indicator visible, then start animating, then turn of wrong user/pin label 
_welcomeActivityIndicator.hidden = FALSE; 
[_welcomeActivityIndicator startAnimating]; 
[_wrongUserPin setHidden:YES]; 

// check if username and pin text fields are populated 
if ([_textFieldUsername.text length ] == 0 && [_textFieldPin.text length ] == 0) 
{ 
    [_welcomeActivityIndicator stopAnimating]; 
    [_wrongUserPin setHidden:NO]; 
} 

// CORE DATA  
NSFetchRequest *request= [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext]; 
NSPredicate *predicate =[NSPredicate predicateWithFormat:@"username=%@",self.textFieldUsername.text]; 

//check pin 
Account *pinAccount = [[Account alloc] init]; 
[pinAccount.password isEqualToString:_textFieldPin.text]; 

[request setEntity:entity]; 
[request setPredicate:predicate]; 

NSError *error = nil; 

NSArray *array = [_managedObjectContext executeFetchRequest:request error:&error]; 
if (array != nil) { 
    NSUInteger count = [array count]; // may be 0 if the object has been deleted. 
    NSLog(@"Username may exist, %@",count); 
} 

else { 
    NSLog(@"Username does not exist."); 
} 

// TODO - put this in proper place - play audio bell if user logs in correctly 
CFBundleRef mainBundle = CFBundleGetMainBundle(); 
CFURLRef soundFileURLRef; 
soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Glass", CFSTR("aiff"), NULL); 
UInt32 soundID; 
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); 
AudioServicesPlaySystemSound(soundID); 

// TODO - put this in proper place - Load ViewControllerHome 
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
ViewControllerHome *home = (ViewControllerHome *)[storyboard instantiateViewControllerWithIdentifier:@"Home"]; 
[self presentModalViewController:home animated:YES]; 

} 

帳戶AccountBase類文件^h如下所示:

Account.hhttp://pastie.org/4149299

Account.mhttp://pastie.org/4149296

AccountBase.hhttp://pastie.org/4149301

AccountBase.mhttp://pastie.org/4149302

我將不勝感激任何意見或想法,並感謝您的閱讀。

回答

1

我可以用下面的代碼來完成登錄過程。

- (IBAction)processLogin:(id)sender { 

// hide keyboard 
[_textFieldUsername resignFirstResponder]; 
[_textFieldPin resignFirstResponder]; 


// First - make activity indicator visible, then start animating, then turn of wrong user/pin label 
_welcomeActivityIndicator.hidden = FALSE; 
[_welcomeActivityIndicator startAnimating]; 
[_wrongUserPin setHidden:YES]; 

// check if username and pin text fields are populated 
if ([_textFieldUsername.text length ] == 0 && [_textFieldPin.text length ] == 0) 
{ 
    [_welcomeActivityIndicator stopAnimating]; 
    [_wrongUserPin setHidden:NO]; 
} 

// CORE DATA 
NSFetchRequest *request = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Account" inManagedObjectContext:_managedObjectContext]; 

// set entity for request 
[request setEntity:entity]; 

// filter results using a predicate 
NSPredicate *pred =[NSPredicate predicateWithFormat:(@"username = %@"), _textFieldUsername.text]; 

// set predicate for the request 
[request setPredicate:pred]; 

NSError *error = nil; 

// store DB usernames in results array 
NSArray *results = [_managedObjectContext executeFetchRequest:request error:&error]; 

NSLog(@"The returned results are %@",results); 


// check text field against results stored in DB 
for (Account *anAccount in results) { 
    if ([anAccount.username isEqualToString:_textFieldUsername.text]){ 
     NSLog(@"Your username exists"); 
     if ([anAccount.password isEqualToString:_textFieldPin.text]){ 
      NSLog(@"Your pin is correct"); 

      // TODO - put this in proper place - play audio bell if user logs in correctly 
      CFBundleRef mainBundle = CFBundleGetMainBundle(); 
      CFURLRef soundFileURLRef; 
      soundFileURLRef = CFBundleCopyResourceURL(mainBundle, (CFStringRef) @"Glass", CFSTR("aiff"), NULL); 
      UInt32 soundID; 
      AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID); 
      AudioServicesPlaySystemSound(soundID); 

      // TODO - put this in proper place - Load ViewController(Root)Home 
      if([anAccount.username isEqualToString:@"root"]) 
      { 
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
       ViewControllerRootHome *roothome = (ViewControllerRootHome *)[storyboard instantiateViewControllerWithIdentifier:@"rootHome"]; 
       [self presentModalViewController:roothome animated:YES]; 
      } 
      else { 
       UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil]; 
       ViewControllerHome *home = (ViewControllerHome *)[storyboard instantiateViewControllerWithIdentifier:@"Home"]; 
       [self presentModalViewController:home animated:YES]; 
      } 
     } 
     else { 
      NSLog(@"Your pin is wrong"); 
      [_welcomeActivityIndicator stopAnimating]; 
      [_wrongUserPin setHidden:NO]; 
      } 
     } 
    else { 
     NSLog(@"Your username was not found"); 
     [_welcomeActivityIndicator stopAnimating]; 
     [_wrongUserPin setHidden:NO]; 
     } 
    } 

} 
0

我相信你的錯誤使用核心數據。它並不意味着像sqlite一樣使用,始終運行始終可以查詢。 (http://cocoawithlove.com/2010/02/differences-between-core-data-and.html)

你正試圖保留用戶的數據庫,但你也過於複雜。而在內存中的應用程序可以使用自己的對象的NSMutableArray來跟蹤用戶名,標籤

所以,你將有你的類:

interface Alcoholic : NSObject 

@property(nonatomic,retain)NSString * username; 
@property(nonatomic,retain)NSString * pin; 
@property(nonatomic,retain)NSString * tab; 
@end 

在代碼,增加了這些,將有一節可變數組

NSMutableArray * alcoholics = [[NSMutableArray alloc]init]; 

- (IBAction)processLogin:(id)sender { 

int i = 0; 
while (i<alcoholics.count) 
{ 
Aloholic = [alcoholics objectAtIndex:i]; 
if(self.textFieldUsername.text == Aloholic.username) 
    NSLog(@"Username may exist, %@",count); 
} 

else { 
    NSLog(@"Username does not exist."); 
} 
} 

等等

因爲我敢肯定,你的下一個問題是如何讓它持續並在核心數據的用武之地多數民衆贊成。

在應用程序委託獲得酗酒陣列,並且保存到核心數據時,應用程序打開和關閉

-(void)save 
{ 
    //use core data to save needed data persitently 

    self.Values = [NSEntityDescription 
         insertNewObjectForEntityForName:@"processLoginClass" 
         inManagedObjectContext:[self managedObjectContext]]; 

      self.Values.alcoholics = self.processLoginClass.alcoholics; 

    } 

    NSError *error; 
    if (![[self managedObjectContext] save:&error]) 
    { 
      NSLog(@"Couldn't save state information: %@", [error localizedDescription]); 
    } 
} 

-(void)load 
{ 
    //use core data to load needed data 

    NSError *error; 
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Values" 
               inManagedObjectContext:[self managedObjectContext]]; 
    [fetchRequest setEntity:entity]; 

    NSArray *fetchedObjects = [[self managedObjectContext] executeFetchRequest:fetchRequest error:&error]; 
    for (processLoginClass *info in fetchedObjects) 
    { 
      [self.processLoginClass setAlcholics:info.alcholics]; 
    }  
}