美文网首页
Swift 隐私条款弹框及查看隐私条款

Swift 隐私条款弹框及查看隐私条款

作者: Wynter_Wang | 来源:发表于2021-12-18 10:26 被阅读0次

为什么要写这么简单的东西呢?因为以后每个APP启动这是一个必须功能,总结出来后面就不用每个应用都去重新写

效果图.png

实现代码

import UIKit

let kPrivacyState = "kPrivacyState"

class WTPrivacyDialogViewController: UIViewController {
    
    private var checkBtn: UIButton!
    
    var actionBlock: ((Int) -> ())?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.75)
        setupUI()
    }
    
    func setupUI() {
        
        let contentView = UIView()
        contentView.backgroundColor = .white
        contentView.layer.cornerRadius = 8
        contentView.layer.masksToBounds = true
        view.addSubview(contentView)
        
        let titleLb = UILabel()
        titleLb.text = "隐私条款提示"
        titleLb.font = UIFont.boldSystemFont(ofSize: 18)
        contentView.addSubview(titleLb)
        
        let style = NSMutableParagraphStyle()
        style.lineSpacing = 5
        
        let aStr = NSMutableAttributedString(string: "为了更好的保护您的权益,同时遵守相关督管要求,我们将通过《隐私政策》向您说明我们会如何搜集、存储、保护和使用您的信息。", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor : RGBA(r: 102, g: 102, b: 102, a: 1), NSAttributedString.Key.paragraphStyle: style])
        let subtitleLb = UILabel()
        subtitleLb.numberOfLines = 0
        subtitleLb.textColor = RGBA(r: 102, g: 102, b: 102, a: 1)
        subtitleLb.attributedText = aStr
        subtitleLb.textAlignment = .center
        contentView.addSubview(subtitleLb)
        
        let button = UIButton(type:.custom)
        button.setImage(UIImage(named: "checkbox-blank-circle-line"), for: .normal)
        button.setImage(UIImage(named: "checkbox-circle-line"), for: .selected)
        button.addTarget(self, action:#selector(checkAction(_:)), for:.touchUpInside)
        checkBtn = button
        contentView.addSubview(checkBtn)
        
        let agreeStr = NSMutableAttributedString(string: "已查看并同意《隐私政策》", attributes: [NSAttributedString.Key.font : UIFont.systemFont(ofSize: 14), NSAttributedString.Key.foregroundColor : RGBA(r: 102, g: 102, b: 102, a: 1)])
        agreeStr.yy_setTextHighlight(NSRange(location: 6, length: 6), color: .blue, backgroundColor: .clear) { containerView, text, range, rect in
            let vc = WTPrivacyContentViewController()
            vc.title = "隐私政策"
            let nav = UINavigationController(rootViewController: vc)
            self.present(nav, animated: false, completion: nil)
        }
        
        let agreeLb = YYLabel()
        agreeLb.attributedText = agreeStr
        contentView.addSubview(agreeLb)
        
        let agreeBtn = UIButton(type: .custom)
        agreeBtn.setTitle("同意", for: .normal)
        agreeBtn.setTitleColor(.white, for: .normal)
        agreeBtn.backgroundColor = .blue
        agreeBtn.layer.cornerRadius = 18
        agreeBtn.layer.masksToBounds = true
        agreeBtn.addTarget(self, action:#selector(agreeAction(_:)), for:.touchUpInside)
        contentView.addSubview(agreeBtn)
        
        let regectBtn = UIButton(type: .custom)
        regectBtn.addTarget(self, action:#selector(regectAction(_:)), for:.touchUpInside)
        regectBtn.setTitle("不同意并退出app", for: .normal)
        regectBtn.setTitleColor(.blue, for: .normal)
        contentView.addSubview(regectBtn)
        
        contentView.snp_makeConstraints { make in
            make.centerY.equalTo(view)
            make.right.equalTo(-20)
            make.left.equalTo(20)
            make.bottom.equalTo(regectBtn.snp_bottom).offset(24)
        }
        
        titleLb.snp_makeConstraints { make in
            make.centerX.equalTo(contentView)
            make.top.equalTo(20)
            make.height.equalTo(26)
        }
        
        subtitleLb.snp_makeConstraints { make in
            make.top.equalTo(titleLb.snp_bottom).offset(8)
            make.right.equalTo(-24)
            make.left.equalTo(24)
            make.height.greaterThanOrEqualTo(60)
        }
        
        checkBtn.snp_makeConstraints { make in
            make.top.equalTo(subtitleLb.snp_bottom).offset(24)
            make.left.equalTo(24)
            make.size.equalTo(CGSize(width: 20, height: 20))
        }
        
        agreeLb.snp_makeConstraints { make in
            make.centerY.equalTo(checkBtn)
            make.height.equalTo(20)
            make.left.equalTo(checkBtn.snp_right).offset(2)
        }
        
        agreeBtn.snp_makeConstraints { make in
            make.top.equalTo(checkBtn.snp_bottom).offset(20)
            make.height.equalTo(36)
            make.right.equalTo(-24)
            make.left.equalTo(24)
        }
        
        regectBtn.snp_makeConstraints { make in
            make.top.equalTo(agreeBtn.snp_bottom).offset(12)
            make.height.equalTo(22)
            make.centerX.equalTo(agreeBtn)
        }
        
    }
    
    @objc func checkAction(_ button:UIButton){
        button.isSelected = !button.isSelected
    }
    
    @objc func agreeAction(_ button:UIButton){
        if !checkBtn.isSelected {
            let hub = MBProgressHUD(view: view)
            hub?.mode = .text
            hub?.labelText = "请先勾选并同意隐私政策"
            view.addSubview(hub!)
            hub?.show(animated: true, whileExecuting: {
                sleep(1)
            }, completionBlock: {
                hub?.removeFromSuperview()
            })
            return
        }
        
        UserDefaults.standard.setValue(true, forKey: kPrivacyState)
        if let block = actionBlock {
            block(0)
        }
        self.dismiss(animated: true, completion: nil)
    }
    
    @objc func regectAction(_ button:UIButton){
        // 可以增加再次确认在退出
        exit(0)
    }
}

弹出隐私条款更新

let vc = IRCPrivacyDialogViewController()
vc.modalTransitionStyle = .crossDissolve
vc.modalPresentationStyle = .custom
vc.actionBlock = {index in
    print("点击了按钮\(index)")
}
self.present(vc, animated: false, completion: nil)

小问题

这里遇到一个小问题,就是弹框中点击隐私政策后跳转问题

我这里使用prsent一个UINavigationController

let vc = WTPrivacyContentViewController()
vc.title = "隐私政策"
let nav = UINavigationController(rootViewController: vc)
self.present(nav, animated: false, completion: nil)

使用这种方式可以保持和原有导航栏一致,需要注意在WTPrivacyContentViewController中需要自定义返回按钮,使用dismiss方式返回

let goback = UIButton(type: .custom)
goback.setImage(UIImage(named: "goback_dark"), for: .normal)
goback.frame = CGRect(x: 0, y: 0, width: 32, height: 32)
goback.contentHorizontalAlignment = .left
goback.addTarget(self, action: #selector(backAction), for: .touchUpInside)
self.navigationItem.leftBarButtonItem = UIBarButtonItem(customView: goback)

@objc func backAction() {
    if self.navigationController?.viewControllers.count > 1 {
        self.popViewController(animated: true)
    } else {
        self.dismiss(animated: true, completion: nil)
    }
 }

其实代码实现还是没有xib实现快,看个人喜好吧,喜欢纯代码的就纯代码,喜欢xib的就用xib

我是大自然的搬砖工,希望能够帮到你,谢谢~

相关文章

  • Swift 隐私条款弹框及查看隐私条款

    为什么要写这么简单的东西呢?因为以后每个APP启动这是一个必须功能,总结出来后面就不用每个应用都去重新写 实现代码...

  • 隐私协议

    平台非常重视用户个人信息及隐私权的保护。本本app个人信息保护及隐私权条款(“本隐私权条款”)适用于本app提供的...

  • 隐私条款(Copyright declaration)

    隐私条款Copyright declaration) ...

  • 隐私条款

    本应用尊重并保护所有使用服务用户的个人隐私权。为了给您提供更准确、更有个性化的服务,本应用会按照本隐私权政策的规定...

  • 隐私条款

    美颜美妆相机尊重并保护所有使用美颜美妆相机网络服务用户的个人隐私权。为了给您提供更准确、更有个性化的服务,美颜美妆...

  • 隐私条款

    欢迎访问工资计算器!我们以本隐私声明对访问者隐私保护的许诺。 当您使用工资计算器服务,即表示您信赖我们对您的...

  • 隐私条款

    一、引言 我们重视用户的隐私。您在使用我们的服务时,我们可能会收集和使用您的相关信息。我们希望通过本《隐私政策》向...

  • 隐私条款

    本《隐私政策》适用于我们提供的一切服务。当您使用我们任何单项服务时,您同意接受本《隐私政策》以及我们在该单项服务中...

  • 隐私条款

    本应用尊重并保护所有使用服务用户的个人隐私权。为了给您提供更准确、更有个性化的服务,本应用会按照本隐私权政策的规定...

  • 隐私条款

    本软件尊重并保护所有使用服务用户的个人隐私权。为了给您提供更准确、更有个性化的服务,本软件会按照本隐私权政策的规定...

网友评论

      本文标题:Swift 隐私条款弹框及查看隐私条款

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