This repository was archived by the owner on Jul 1, 2024. It is now read-only.
forked from bkook/BKPasscodeView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBKPasscodeLockScreenManager.m
More file actions
118 lines (88 loc) · 3.63 KB
/
Copy pathBKPasscodeLockScreenManager.m
File metadata and controls
118 lines (88 loc) · 3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
//
// BKPasscodeLockScreenManager.m
// BKPasscodeViewDemo
//
// Created by Byungkook Jang on 2014. 8. 2..
// Copyright (c) 2014년 Byungkook Jang. All rights reserved.
//
#import "BKPasscodeLockScreenManager.h"
#import "BKPasscodeViewController.h"
static BKPasscodeLockScreenManager *_sharedManager;
@interface BKPasscodeLockScreenManager ()
@property (strong, nonatomic) UIWindow *mainWindow;
@property (strong, nonatomic) UIWindow *lockScreenWindow;
@property (strong, nonatomic) UIView *blindView;
@end
@implementation BKPasscodeLockScreenManager
+ (BKPasscodeLockScreenManager *)sharedManager
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedManager = [[BKPasscodeLockScreenManager alloc] init];
});
return _sharedManager;
}
- (void)showLockScreen:(BOOL)animated
{
NSAssert(self.delegate, @"delegate is not assigned.");
if (self.lockScreenWindow && self.lockScreenWindow.rootViewController) {
return;
}
if ([self.delegate respondsToSelector:@selector(lockScreenManagerShouldShowLockScreen:)]) {
if (NO == [self.delegate lockScreenManagerShouldShowLockScreen:self]) {
return;
}
}
// get the main window
self.mainWindow = [[UIApplication sharedApplication] keyWindow];
// dismiss keyboard before showing lock screen
[self.mainWindow.rootViewController.view endEditing:YES];
// add blind view
UIView *blindView;
if ([self.delegate respondsToSelector:@selector(lockScreenManagerBlindView:)]) {
blindView = [self.delegate lockScreenManagerBlindView:self];
}
if (nil == blindView) {
blindView = [[UIView alloc] init];
blindView.backgroundColor = [UIColor whiteColor];
}
blindView.frame = self.mainWindow.bounds;
blindView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.mainWindow addSubview:blindView];
self.blindView = blindView;
// set dummy view controller as root view controller
BKPasscodeDummyViewController *dummyViewController = [[BKPasscodeDummyViewController alloc] init];
UIWindow *lockScreenWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
lockScreenWindow.windowLevel = self.mainWindow.windowLevel + 1;
lockScreenWindow.rootViewController = dummyViewController;
lockScreenWindow.backgroundColor = [UIColor clearColor];
[lockScreenWindow makeKeyAndVisible];
// present lock screen
UIViewController *lockScreenViewController = [self.delegate lockScreenManagerPasscodeViewController:self];
if (animated) {
blindView.hidden = YES;
}
[lockScreenWindow.rootViewController presentViewController:lockScreenViewController animated:animated completion:^{
blindView.hidden = NO;
}];
self.lockScreenWindow = lockScreenWindow;
[lockScreenViewController.view.superview bringSubviewToFront:lockScreenViewController.view];
dummyViewController.delegate = self;
}
- (void)dummyViewControllerWillAppear:(BKPasscodeDummyViewController *)aViewController
{
// remove blind view
[self.blindView removeFromSuperview];
self.blindView = nil;
}
- (void)dummyViewControllerDidAppear:(BKPasscodeDummyViewController *)aViewController
{
if ([UIView instancesRespondToSelector:@selector(tintColor)]) {
self.lockScreenWindow = nil;
} else {
[self performSelector:@selector(setLockScreenWindow:) withObject:nil afterDelay:0.1f]; // workaround for wired dealloc on iOS 6
}
[self.mainWindow makeKeyAndVisible];
self.mainWindow = nil;
}
@end