[英]Implementing a lock screen for iOS app
我正在開發一個需要“鎖定屏幕”的應用程序,用戶必須輸入密碼才能訪問存儲在應用程序中的數據。
每當應用程序啟動時以及用戶不活動幾分鍾后,都應顯示此鎖定屏幕。
目前我的故事板中有一個單獨的UIViewController,它沒有連接到故事板中的任何其他視圖。 我知道我可以通過發送instantiateViewControllerWithIdentifier到[self storyboard]來訪問這個視圖控制器。 Hoewever這不適用於app委托。
但是我需要在應用程序委托嘗試打開數據庫之前從應用程序委托調用鎖定屏幕。
實現這個的好方法是什么?
這可能是一個遲到的答案,但我必須使用Storyboard為我的一個客戶實現相同的requeriment。 您可以從這里下載完整的項目 。 希望這可能對某人有所幫助。
我創建了兩個Viewcontrollers,一個是MainView,另一個需要四位數代碼才能被解除。 我在MainView和BlockKeypadViewController之間創建了一個模態segue,並將其命名為“lockItSegue”。 在MainView上我插入了一個RoundRectButton,它調用了“lockIt”方法。 該方法只執行segue“lockItSegue”。
- (IBAction)lockIt:(id)sender
{
[self performSegueWithIdentifier:@"lockItSegue" sender:self];
}
在BlockKeypadViewController.h上,我創建了
//
// BlockKeypadViewController.h
// Consultablet
//
// Created by EDGARD AGUIRRE ROZO on 22/03/13.
// Copyright (c) 2013 Colombia Creative Software. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
@interface BlockKeypadViewController : UIViewController
@property (strong, nonatomic) IBOutlet UILabel *wrongCodeLabel;//*codigoIncorrectoLabel;
@property (strong, nonatomic) IBOutlet UILabel *tryAgainLabel;//*trateNuevamenteLabel;
@property (strong, nonatomic) IBOutlet UILabel *digit1;//*caracter1;
@property (strong, nonatomic) IBOutlet UILabel *digit2;//*caracter2;
@property (strong, nonatomic) IBOutlet UILabel *digit3;//*caracter3;
@property (strong, nonatomic) IBOutlet UILabel *digit4;//*caracter4;
@property int counter;//contador;
@property int codeIn;//claveIngresado;
@property int unlockCode;//claveDesbloqueo;
-(void) calculate:(int)number;//calcular:(int)numero;
- (IBAction)one:(id)sender;//uno:(id)sender;
- (IBAction)two:(id)sender;//dos:(id)sender;
- (IBAction)three:(id)sender;//tres:(id)sender;
- (IBAction)four:(id)sender;//cuatro:(id)sender;
- (IBAction)five:(id)sender;//cinco:(id)sender;
- (IBAction)six:(id)sender;//seis:(id)sender;
- (IBAction)seven:(id)sender;//siete:(id)sender;
- (IBAction)eight:(id)sender;//ocho:(id)sender;
- (IBAction)nine:(id)sender;//nueve:(id)sender;
- (IBAction)zero:(id)sender;//cero:(id)sender;
- (IBAction)cancel:(id)sender;//cancel:(id)sender;
-(void)checkCode;//verificarClave;
@end
在BlockKeypadViewController.m上:
//
// BlockKeypadViewController.m
// Consultablet
//
// Created by EDGARD AGUIRRE ROZO on 22/03/13.
// Copyright (c) 2013 Colombia Creative Software. All rights reserved.
//
#import "BlockKeypadViewController.h"
@implementation UILabel (My2)
//- (void)setImage:(UIImage *)image forState:(UIControlState)state animated:(BOOL)animated
- (void)setHidden:(BOOL)hidden animated:(BOOL)animated
{
//[self setImage:image forState:state];
[self setHidden:hidden];
if (animated)
{
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setFillMode:kCAFillModeBoth];
[animation setDuration:.3];
[[self layer] addAnimation:animation forKey:@"UILabelSetAnimationKey"];
}
}
- (void)setHidden:(BOOL)hidden animated:(BOOL)animated seconds:(int)seconds
{
//[self setImage:image forState:state];
[self setHidden:hidden];
if (animated)
{
CATransition *animation = [CATransition animation];
[animation setType:kCATransitionFade];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[animation setFillMode:kCAFillModeBoth];
[animation setDuration:seconds];
[[self layer] addAnimation:animation forKey:@"UILabelSetAnimationKey"];
}
}
@end
@interface BlockKeypadViewController ()
@end
@implementation BlockKeypadViewController
@synthesize digit1,digit2,digit3,digit4,counter,codeIn,unlockCode,wrongCodeLabel,tryAgainLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
self.counter = 1;
self.codeIn=0;
self.unlockCode=1234;
NSLog(@"Unlock Code %d",self.unlockCode);
[digit1 setHidden:YES animated:YES];
[digit2 setHidden:YES animated:YES];
[digit3 setHidden:YES animated:YES];
[digit4 setHidden:YES animated:YES];
}
-(void)checkCode
{
if(self.codeIn==self.unlockCode)
{
[self dismissViewControllerAnimated:YES completion:nil];
}
else
{
[wrongCodeLabel setHidden:NO animated:YES seconds:.1];
[tryAgainLabel setHidden:NO animated:YES seconds:.1];
[self.digit1 setHidden:YES animated:YES];
[self.digit2 setHidden:YES animated:YES];
[self.digit3 setHidden:YES animated:YES];
[self.digit4 setHidden:YES animated:YES];
self.codeIn = 0;
NSLog(@"Wrong Code");
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void) calculate:(int)number
{
if(self.counter==1)
{
[wrongCodeLabel setHidden:YES animated:YES seconds:.2];
[tryAgainLabel setHidden:YES animated:YES seconds:.2];
[self.digit1 setHidden:NO animated:YES];
self.codeIn = number*1000;
}
if(self.counter==2)
{
[self.digit2 setHidden:NO animated:YES];
self.codeIn = self.codeIn+number*100;
}
if(self.counter==3)
{
[self.digit3 setHidden:NO animated:YES];
self.codeIn = self.codeIn+number*10;
}
if(self.counter==4)
{
[self.digit4 setHidden:NO animated:YES];
self.codeIn = self.codeIn+number;
[self performSelector:@selector(checkCode) withObject:nil afterDelay:0.2];
}
if(self.counter<4)
{
self.counter++;
}
else
{
self.counter=1;
}
}
- (IBAction)one:(id)sender
{
[self calculate:1];
}
- (IBAction)two:(id)sender
{
[self calculate:2];
}
- (IBAction)three:(id)sender
{
[self calculate:3];
}
- (IBAction)four:(id)sender
{
[self calculate:4];
}
- (IBAction)five:(id)sender
{
[self calculate:5];
}
- (IBAction)six:(id)sender
{
[self calculate:6];
}
- (IBAction)seven:(id)sender
{
[self calculate:7];
}
- (IBAction)eight:(id)sender
{
[self calculate:8];
}
- (IBAction)nine:(id)sender
{
[self calculate:9];
}
- (IBAction)zero:(id)sender
{
[self calculate:0];
}
- (IBAction)cancel:(id)sender
{
[self.digit1 setHidden:YES animated:YES];
[self.digit2 setHidden:YES animated:YES];
[self.digit3 setHidden:YES animated:YES];
[self.digit4 setHidden:YES animated:YES];
self.codeIn = 0;
self.counter = 1;
}
@end
要從appDelegate在HomeVC頂部顯示lockVC,請使用以下命令:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
HomeVC *hvc = [storyboard instantiateViewControllerWithIdentifier:@"HomeVC"];
self.window.rootViewController = hvc;
[self.window makeKeyAndVisible];
LockVC *lvc = [storyboard instantiateViewControllerWithIdentifier:@"LockVC"]
[self.window.rootViewController presentViewController:lvc animated:YES completion:nil];
沒有必要(我實際上不建議將它用於此海豚)使用AppDelegate
來實現這個或任何LockScreen
解決方案。 您應該從RootViewController
到LockScreen
創建一個segue並根據需要命名(例如lockItSegue
)。 然后從按鈕動作調用[self performSegueWithIdentifier:@"lockItSegue" sender:self];
上的邏輯( BlockKeypadViewController.m
所述的) LockScreen
(一旦用戶已經輸入了正確的代碼),呼叫[self dismissViewControllerAnimated:YES completion:nil];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.