首先截屏是操作系統級別的操作,app上的代碼無法靈活到某個頁面的禁止,可以配置整個app禁止截屏的操作,但是不符合需求。所以只能退而求其次,改變截屏出來的圖片顯示自己想顯示的內容。
此處屏蔽截屏內容的原理如下
利用textField在secureTextEntry=YES截屏時,其內容被隱藏的特性,將隱藏的內容加到其上面截屏時不顯示,但是此方法只能iOS13以上使用。
- (void)viewDidLoad {
[super viewDidLoad];
if (@available(iOS 13.2, *)) {
GDForbidScreenShotView *tempView = [[GDForbidScreenShotView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
[tempView addSubview:self.view];
self.view = tempView;
}
self.view.backgroundColor = UIColor.whiteColor;
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(150, 80, 100, 40)];
button.backgroundColor = UIColor.blueColor;
[button setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
button.titleLabel.font = [UIFont systemFontOfSize:15];
[button setTitle:@"這是個按鈕" forState:(UIControlStateNormal)];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
[self.view addSubview:button];
}
之前發現一個問題就是textfeild響應了子view的輸入事件,導致頁面錯亂問題,設置textFieldShouldBeginEditing返回值為NO,使得當前的textField不能編輯,若是使用textField.enable=NO,則會影響子view的交互。
- (void)setupUI {
//此處textField在secureTextEntry=YES截屏時,其內容被隱藏的特性,使加到其上面的view都截屏上不顯示
[self addSubview:self.textField];
self.textField.subviews.firstObject.userInteractionEnabled = YES;
[self.textField.subviews.firstObject addSubview:self.clearView];
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
}
- (void)addSubview:(UIView *)view {
[super addSubview:view];
if (self.textField != view) {
[self.clearView addSubview:view];
}
}
- (void)keyboardWillShow:(NSNotification *)noti {
if (self.textField.isFirstResponder) {
[self.textField resignFirstResponder];
self.textField.subviews.firstObject.userInteractionEnabled = YES;
}
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
return NO;
}
在此記錄一下,看到的希望能幫到你。本篇GitHubDemo傳送陣在此。