首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iOS中终止并再次启动应用程序时,NSNotificationCenter不会触发

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

Stack Overflow用户
提问于 2019-11-29 01:23:44
回答 1查看 405关注 0票数 2

我有一个物流应用程序,它要求禁用屏幕录制,我不希望人们使用新的iOS-11功能来录制带有敏感数据的屏幕并将其公之于众。由于禁用屏幕录制在iOS中是不可能的,所以我尝试了一种解决方法,检测iOS11屏幕录制功能的打开或关闭。我使用了isCaptured和UIScreenCapturedDidChange通知。代码如下:

代码语言:javascript
复制
   - (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;
 }

所以逻辑是,当屏幕记录开始时,通知被触发,并且在记录发生时将黑色置于视频上的逻辑被移除,而当记录停止时,黑色被移除。当应用程序移动到后台、活动、非活动和前台场景时,这一点工作得很好。但是,当应用程序在屏幕录制仍在进行时被终止(终止),以及当我们再次启动应用程序时屏幕录制仍在进行时,黑色不会覆盖屏幕。那么如何在选择器方法工作的情况下再次调用通知选择器呢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-29 01:46:00

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

UIWindow+ScreenCapture.h:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#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:

代码语言:javascript
复制
#import <UIKit/UIKit.h>

@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (nullable, strong, nonatomic) UIWindow *window;

@end

AppDelegate.m:

代码语言:javascript
复制
#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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59093835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档