gpt4 book ai didi

ios - 当应用程序终止并在 iOS 中再次启动时,NSNotificationCenter 不会触发

转载 作者:行者123 更新时间:2023-11-29 11:25:28 25 4
gpt4 key购买 nike

我有一个物流应用程序要求禁用屏幕录制,我不希望人们使用新的 iOS-11 功能来录制包含敏感数据的屏幕并将其公开。由于在 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 场景时,这工作正常。但是,当应用程序被终止(终止)而屏幕录制仍在进行时,以及当我们再次启动应用程序而屏幕录制仍在进行时,黑色不会覆盖屏幕。那么如何使用选择器方法再次调用通知选择器

最佳答案

检查 application:didFinishLaunchingWithOptions: 中屏幕的 isCaptured 属性的值,并相应地显示或隐藏覆盖 View 。

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 - 当应用程序终止并在 iOS 中再次启动时,NSNotificationCenter 不会触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59093835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com