[英]Handling an iOS login segue
逐步看到下面的身份驗證后,我將如何逐步隔離用戶?
//curent code in viewcontroller2.h
-(IBAction)btnLoginRegisterTapped:(UIButton*)sender {
//form fields validation
if (fldUsername.text.length < 4 || fldPassword.text.length < 4) {
[UIAlertView error:@"Enter username and password over 4 chars each."];
return;
}
//salt the password
NSString* saltedPassword = [NSString stringWithFormat:@"%@%@", fldPassword.text, kSalt];
//prepare the hashed storage
NSString* hashedPassword = nil;
unsigned char hashedPasswordData[CC_SHA1_DIGEST_LENGTH];
//hash the pass
NSData *data = [saltedPassword dataUsingEncoding: NSUTF8StringEncoding];
if (CC_SHA1([data bytes], [data length], hashedPasswordData)) {
hashedPassword = [[NSString alloc] initWithBytes:hashedPasswordData length:sizeof(hashedPasswordData) encoding:NSASCIIStringEncoding];
} else {
[UIAlertView error:@"Password can't be sent"];
return;
}
//check whether it's a login or register
NSString* command = (sender.tag==1)?@"register":@"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command", fldUsername.text, @"username", hashedPassword, @"password", nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];
if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
[[API sharedInstance] setUser: res];
//this doesn't dismiss anything; its leftover from another tutorial =)
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil];
//show message to the user
[[[UIAlertView alloc] initWithTitle:@"Logged in" message:[NSString stringWithFormat:@"Welcome %@",[res objectForKey:@"username"]] delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
} else {
//error
[UIAlertView error:[json objectForKey:@"error"]];
}
}];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
是否可以在情節提要板上連接足夠的序列並將其置於didrecevememory警告是否足夠?
if (![[API sharedInstance] isAuthorized]) {
[self performSegueWithIdentifier:@"ShowLogin" sender:nil];
}
如何從情節提要中創建標記? 我已經看過這3個鏈接,但是在登錄后創建序列時仍然需要一個堅定的答案。 成功登錄后的自定義segue http://www.raywenderlich.com/50308/storyboards-tutorial-in-ios-7-part-1 https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/CreatingCustomSegues/ CreatingCustomSegues.html
performSegueWithIdentifier
是正確的。 您需要確保在主線程上執行此操作-
NSString* command = (sender.tag==1)?@"register":@"login";
NSMutableDictionary* params =[NSMutableDictionary dictionaryWithObjectsAndKeys:command, @"command", fldUsername.text, @"username", hashedPassword, @"password", nil];
//make the call to the web API
[[API sharedInstance] commandWithParams:params onCompletion:^(NSDictionary *json) {
//result returned
NSDictionary* res = [[json objectForKey:@"result"] objectAtIndex:0];
if ([json objectForKey:@"error"]==nil && [[res objectForKey:@"IdUser"] intValue]>0) {
[[API sharedInstance] setUser: res];
dispatch_async(dispatch_get_main_queue(),^{
[self performSegueWithIdentifier:@"someIdentifier" sender:self];
});
} else {
//error
[UIAlertView error:[json objectForKey:@"error"]];
}
}];
您將需要確保“ someIdentifier”是您登錄場景中下一個場景中segue的標識符。 要創建segue,請ctrl從登錄場景中的View Controller圖標拖到目標場景,並為segue提供一個標識符。
如果要更改序列的視覺外觀,則僅需要創建自定義序列。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.