当应用程序终止并在 iOS 中再次启动时,NSNotificationCenter 不会触发
NSNotificationCenter not firing when application is terminated and launched again in iOS
我有一个物流应用程序要求禁用屏幕录制,我不希望人们使用新的 iOS-11 功能来录制带有敏感数据的屏幕并让他们 public。由于在 iOS 中无法禁用屏幕录制,因此我尝试通过检测 iOS11 屏幕录制功能打开或关闭来解决这个问题。我使用了 isCaptured 和 UIScreenCapturedDidChange 通知。代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (@available(iOS 11.0, *)) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenCaptureChanged) name:UIScreenCapturedDidChangeNotification object:nil];
}
return YES;
}
所以逻辑是当屏幕录制开始时触发通知,并且在录制时将黑色放在视频上的逻辑发生,当录制停止时黑色被删除。当应用程序移动到 background 、 active 、 inactive 和 foreground 场景时,这工作正常。但是,当应用程序被终止(终止)而屏幕录制仍在进行时,以及当我们再次启动应用程序而屏幕录制仍在进行时,黑色不会覆盖屏幕。那么如何使用选择器方法再次调用通知选择器
检查 isCaptured
属性 在 application:didFinishLaunchingWithOptions:
中的值,并相应地显示或隐藏叠加视图。
UIWindow+ScreenCapture.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIWindow (ScreenCapture)
- (void)showOrHideContentDependingOnCapturedState;
- (void)hideContent;
- (void)showContent;
@end
NS_ASSUME_NONNULL_END
UIWindow+ScreenCapture.m:
#import "UIWindow+ScreenCapture.h"
@implementation UIWindow (ScreenCapture)
- (void)showOrHideContentDependingOnCapturedState {
if (self.screen.isCaptured) {
[self hideContent];
} else {
[self showContent];
}
}
- (void)hideContent {
UIView *blackView = [self viewWithTag:1234];
if (!blackView) {
blackView = [[UIView alloc] initWithFrame:self.bounds];
}
blackView.tag = 1234;
blackView.backgroundColor = UIColor.blackColor;
blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// This is important! If this is not set, then any new view added to
// the window (like the root view controller's view) will be overtop of
// `blackView`, which we don't want.
blackView.layer.zPosition = 10000;
[self addSubview:blackView];
}
- (void)showContent {
[[self viewWithTag:1234] removeFromSuperview];
}
@end
AppDelegate.h:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nullable, strong, nonatomic) UIWindow *window;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "UIWindow+ScreenCapture.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkIsBeingRecorded)
name:UIScreenCapturedDidChangeNotification
object:nil];
[self.window showOrHideContentDependingOnCapturedState];
return YES;
}
- (void)checkIsBeingRecorded {
for (UIWindow *window in UIApplication.sharedApplication.windows) {
[window showOrHideContentDependingOnCapturedState];
}
}
@end
我有一个物流应用程序要求禁用屏幕录制,我不希望人们使用新的 iOS-11 功能来录制带有敏感数据的屏幕并让他们 public。由于在 iOS 中无法禁用屏幕录制,因此我尝试通过检测 iOS11 屏幕录制功能打开或关闭来解决这个问题。我使用了 isCaptured 和 UIScreenCapturedDidChange 通知。代码如下:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (@available(iOS 11.0, *)) {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenCaptureChanged) name:UIScreenCapturedDidChangeNotification object:nil];
}
return YES;
}
所以逻辑是当屏幕录制开始时触发通知,并且在录制时将黑色放在视频上的逻辑发生,当录制停止时黑色被删除。当应用程序移动到 background 、 active 、 inactive 和 foreground 场景时,这工作正常。但是,当应用程序被终止(终止)而屏幕录制仍在进行时,以及当我们再次启动应用程序而屏幕录制仍在进行时,黑色不会覆盖屏幕。那么如何使用选择器方法再次调用通知选择器
检查 isCaptured
属性 在 application:didFinishLaunchingWithOptions:
中的值,并相应地显示或隐藏叠加视图。
UIWindow+ScreenCapture.h:
#import <UIKit/UIKit.h>
NS_ASSUME_NONNULL_BEGIN
@interface UIWindow (ScreenCapture)
- (void)showOrHideContentDependingOnCapturedState;
- (void)hideContent;
- (void)showContent;
@end
NS_ASSUME_NONNULL_END
UIWindow+ScreenCapture.m:
#import "UIWindow+ScreenCapture.h"
@implementation UIWindow (ScreenCapture)
- (void)showOrHideContentDependingOnCapturedState {
if (self.screen.isCaptured) {
[self hideContent];
} else {
[self showContent];
}
}
- (void)hideContent {
UIView *blackView = [self viewWithTag:1234];
if (!blackView) {
blackView = [[UIView alloc] initWithFrame:self.bounds];
}
blackView.tag = 1234;
blackView.backgroundColor = UIColor.blackColor;
blackView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
// This is important! If this is not set, then any new view added to
// the window (like the root view controller's view) will be overtop of
// `blackView`, which we don't want.
blackView.layer.zPosition = 10000;
[self addSubview:blackView];
}
- (void)showContent {
[[self viewWithTag:1234] removeFromSuperview];
}
@end
AppDelegate.h:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (nullable, strong, nonatomic) UIWindow *window;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "UIWindow+ScreenCapture.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkIsBeingRecorded)
name:UIScreenCapturedDidChangeNotification
object:nil];
[self.window showOrHideContentDependingOnCapturedState];
return YES;
}
- (void)checkIsBeingRecorded {
for (UIWindow *window in UIApplication.sharedApplication.windows) {
[window showOrHideContentDependingOnCapturedState];
}
}
@end