美文网首页
iOS16 使用UIPasteControl剪切板继续弹系统弹窗

iOS16 使用UIPasteControl剪切板继续弹系统弹窗

作者: 无比邪恶的小纯洁 | 来源:发表于2022-10-09 17:03 被阅读0次

    iOS 16 中继续使用 UIPasteboard 来获取获取剪贴板的内容时,会弹出系统提示框,询问用户是否允许粘贴。想去掉弹窗,最好使用 UIPasteControl 控件。

    上代码:

    -(void)viewDidLoad{

        [superviewDidLoad];

        if(@available(iOS16.0, *)) {

            [_pasteControl removeFromSuperview];

            [self.nianqieview removeFromSuperview];

            [self getUIPasteControl];

        }else{

            //获取粘贴板的内容

            UIPasteboard*pasteboard = [UIPasteboardgeneralPasteboard];

            str = pasteboard.string;

        }

    }

    -(UIPasteControl *)getUIPasteControl API_AVAILABLE(ios(16.0)){

        UIPasteControlConfiguration *config = [[UIPasteControlConfiguration alloc]init];

        config.baseBackgroundColor = [UIColor greenColor];

        config.baseForegroundColor = [UIColor lightGrayColor];

        config.cornerStyle = UIButtonConfigurationCornerStyleCapsule;

        config.displayMode = UIPasteControlDisplayModeLabelOnly;

        _pasteControl = [[UIPasteControl alloc] initWithConfiguration:config];

        _pasteControl.frame = CGRectMake(self.view.frame.size.width - 120, self.view.frame.size.height - 160, 70, 40);

        UILabel* titleLabel = [[UILabel alloc]init];

        titleLabel.frame=CGRectMake(10,10,self.view.frame.size.width-130,60);

        titleLabel.numberOfLines=2;

        titleLabel.backgroundColor = [UIColor whiteColor];

        titleLabel.text=@"是否允许XXXX读取剪切板内容?";

        UITextView*policyTextView = [[UITextView alloc]initWithFrame:CGRectMake(10,10,self.view.frame.size.width-130,60)];

        policyTextView.delegate=self;

        policyTextView.inputView = [[UIView alloc] initWithFrame:CGRectZero];

        [policyTextView addSubview:titleLabel];

        // 设置target,获取剪切板内容后粘贴的位置

        _pasteControl.target= policyTextView;

        self.nianqieview= [[UIViewalloc]initWithFrame:CGRectMake(10,self.view.frame.size.height-180,self.view.frame.size.width-20,80)];

        self.nianqieview.backgroundColor = [UIColor whiteColor];

        [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector: @selector(performDismiss:)

                                                                                               userInfo:nilrepeats:NO];

        [self.nianqieview addSubview:policyTextView];

        [self.window addSubview:self.nianqieview];

        [self.window addSubview:_pasteControl];

        return _pasteControl;

    }

    - (void)performDismiss: (NSTimer*)timer

    {

       [_pasteControl removeFromSuperview];

      [self.nianqieview removeFromSuperview];

    }

    - (void)textViewDidChange:(UITextView*)textView{

       str = textView.text;

        [self.nianqieview removeFromSuperview];

        [_pasteControl removeFromSuperview];

    }

    - (BOOL)textViewShouldBeginEditing:(UITextView *)textView

    {

        if([textViewisFirstResponder]){

            returnYES;

        }

        return NO;

    }

    刚开始我把UIPasteControl 放到了我自己画的view上,把textview和label都放在了自己的view上,这时点击UIPasteControl的粘切按钮后,还是会弹出系统的提示框,很是苦恼,后来我把UIPasteControl直接放到页面上面,就不再弹系统的提示框了,我也不知道为什么,如果有跟我出现同样状况的,可以试试把UIPasteControl放到Control上试试。

    相关文章

      网友评论

          本文标题:iOS16 使用UIPasteControl剪切板继续弹系统弹窗

          本文链接:https://www.haomeiwen.com/subject/psmoartx.html