笔记篇

iOS 监听录屏、 监测电话呼入

2021-09-26  本文已影响0人  失忆的程序员
导入框架 CoreTelephony.framework
选择CoreTelephony.framework.png 添加CoreTelephony.framework.png

// 头文件
#import <CoreTelephony/CTCallCenter.h>
#import <CoreTelephony/CTCall.h> 

///
@property (nonatomic, strong) CTCallCenter * callCenter;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view. 
    [self monitorCall]; // 
}

// 监测电话
- (void)monitorCall
{
    self.callCenter.callEventHandler = ^(CTCall* call)
    {
        if (call.callState == CTCallStateDisconnected) 
        {
            NSLog(@"电话结束或挂断电话");
        } 
        else if (call.callState == CTCallStateConnected)
        {
            NSLog(@"电话接通");
        } 
        else if (call.callState == CTCallStateIncoming) 
        {
            NSLog(@"来电话");
        } 
        else if (call.callState == CTCallStateDialing) 
        {
            NSLog(@"拨号打电话(在应用内调用打电话功能)");
        }
    };
}

#pragma mark - getter Methods

- (CTCallCenter *)callCenter {
    if (!_callCenter) {
        _callCenter = [[CTCallCenter alloc] init];
    }
    return _callCenter;
}
打印

来电话
电话结束或挂断电话
...
来电话
电话接通
...

监听录屏

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    // 截屏
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(takeScreenTest) name:UIApplicationUserDidTakeScreenshotNotification object:nil];

    // iOS11后中新增了录屏功能
    if (@available(iOS 11.0, *)) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(captureScreenTest) name:UIScreenCapturedDidChangeNotification object:nil];
    }
}    

- (void)takeScreenTest
{
    [self alertTakeScreenWithStr:@"您在截屏,请注意保护个人隐私"];
}
- (void)captureScreenTest
{
    if (@available(iOS 11.0, *)) {
        // 开始录屏时有弹框提示,结束录屏时就不弹框了。
        if (![UIScreen mainScreen].isCaptured) {
            return;
        }
        [self alertTakeScreenWithStr:@"您在录屏,请注意保护个人隐私"];
    }
}
- (void)alertTakeScreenWithStr:(NSString *)str
{
    NSLog(@"监听到截屏或录屏");
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:str preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
    }];
    [alertController addAction:okAction];
    // 如果是页面,用self;如果在appdelegate中,用self.window.rootViewController
    [self presentViewController:alertController animated:YES completion:nil];
}


/// 你的 VC 页面中 

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated]; 
    
    [self addScreen];
    
}
#pragma mark ----- 监测当前设备是否处于录屏状态
- (void)addScreen
{
    // 监测当前设备是否处于录屏状态
    UIScreen * sc = [UIScreen mainScreen];
    if (@available(iOS 11.0, *)) {
        if (sc.isCaptured) {
            NSLog(@"正在录制~~~~~~~~~%d",sc.isCaptured);
// 这里自己调整
            [self captureScreenTest];
        }
    } else {
        // Fallback on earlier versions
    }
    if (@available(iOS 11.0, *)) {
    // 检测到当前设备录屏状态发生变化
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onAppScreenTest_Recording) name:UIScreenCapturedDidChangeNotification  object:nil];
    } else {
        // Fallback on earlier versions
    }
}

上一篇下一篇

猜你喜欢

热点阅读