利用UIWindow實現密碼保護功能,uiwindow密碼保護
需求描述
使用者從任一介面按Home鍵退出,再從後台切換回來時,顯示一個密碼輸入介面,只有使用者輸入正確的密碼,才能進入退出前的介面。
需求分析
由於密碼輸入介面可能從任何應用介面彈出,且需要蓋在所有介面的最上層,所以需喲用UIView來實現
基礎知識
UIWindow的主要作用:
1.作為UIView的最頂層容器,包含應用顯示需要的所有的UIView
2. 傳遞觸摸訊息和鍵盤事件給UIView
具體實現
+ (PasswordInputWindow *)sharedInstance{ static id sharedInstance = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedInstance = [[self alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; }); return sharedInstance;}- (id)initWithFrame:(CGRect)frame{ self = [super initWithFrame:frame]; if (self) {//setup UI}- (void)show{ [self makeKeyWindow]; self.hidden = NO;}
在APPDelegate的下面回調方法中使用該Window
- (void)applicationWillEnterForeground:(UIApplication *)application { [[PasswordInputWindow sharedInstance] show];}
額外的獎勵
通過建立UIWindow,我們可以很容易的實現將某些特定介面置於最上層的效果,但是這種特性最好只用於無法確定該視圖應該在哪個控制器上面,若明確視圖應該出現在哪個控制器上面,則一般選擇在該控制器上建立新的視圖來實現該功能。此外,在一個項目中不應該過多的建立這種UIWindow單例對象,眾所周知,單例會一直保持在記憶體中,得不到釋放!
搬磚工看這裡: http://download.csdn.net/detail/luozhonglan/8608207
******************************************************************************************************************************************************************************************************
posted by 羅大佑子
2015-4-18