Swift 隱私條款彈框及查看隱私條款

為什么要寫這么簡單的東西呢?因為以后每個APP啟動這是一個必須功能渴语,總結(jié)出來后面就不用每個應(yīng)用都去重新寫

效果圖.png

實現(xiàn)代碼

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: "為了更好的保護(hù)您的權(quán)益神妹,同時遵守相關(guān)督管要求拳昌,我們將通過《隱私政策》向您說明我們會如何搜集趴乡、存儲荐吉、保護(hù)和使用您的信息焙糟。", 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){
        // 可以增加再次確認(rèn)在退出
        exit(0)
    }
}

彈出隱私條款更新

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

小問題

這里遇到一個小問題,就是彈框中點擊隱私政策后跳轉(zhuǎn)問題

我這里使用prsent一個UINavigationController

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

使用這種方式可以保持和原有導(dǎo)航欄一致样屠,需要注意在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)
    }
 }

其實代碼實現(xiàn)還是沒有xib實現(xiàn)快,看個人喜好吧瞧哟,喜歡純代碼的就純代碼混巧,喜歡xib的就用xib

我是大自然的搬磚工,希望能夠幫到你勤揩,謝謝~

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末咧党,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子陨亡,更是在濱河造成了極大的恐慌傍衡,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件负蠕,死亡現(xiàn)場離奇詭異蛙埂,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)遮糖,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進(jìn)店門绣的,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人欲账,你說我怎么就攤上這事屡江。” “怎么了赛不?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵惩嘉,是天一觀的道長。 經(jīng)常有香客問我踢故,道長文黎,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任殿较,我火速辦了婚禮耸峭,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘斜脂。我一直安慰自己抓艳,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著玷或,像睡著了一般儡首。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上偏友,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天蔬胯,我揣著相機(jī)與錄音,去河邊找鬼位他。 笑死氛濒,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的鹅髓。 我是一名探鬼主播舞竿,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼窿冯!你這毒婦竟也來了骗奖?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤醒串,失蹤者是張志新(化名)和其女友劉穎执桌,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體芜赌,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡仰挣,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了缠沈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片膘壶。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖洲愤,靈堂內(nèi)的尸體忽然破棺而出香椎,到底是詐尸還是另有隱情,我是刑警寧澤禽篱,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站馍惹,受9級特大地震影響躺率,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜万矾,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一悼吱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧良狈,春花似錦后添、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽馅精。三九已至,卻和暖如春粱檀,著一層夾襖步出監(jiān)牢的瞬間洲敢,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工茄蚯, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留压彭,地道東北人。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓渗常,卻偏偏與公主長得像壮不,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子皱碘,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 1.自定義控件 a.繼承某個控件 b.重寫initWithFrame方法可以設(shè)置一些它的屬性 c.在layouts...
    圍繞的城閱讀 3,391評論 2 4
  • 101 - 框架搭建 大家好询一,從本節(jié)課開始,我會一步一步地教大家把這個頭條這個 app 實現(xiàn)出來尸执。今天的課程主要...
    丶天藍(lán)閱讀 5,797評論 11 35
  • 介紹 UIViewController 可以理解為 App 的界面家凯,負(fù)責(zé)管理 UIView 中顯示的內(nèi)容和用戶的交...
    YungFan閱讀 1,823評論 0 5
  • 前言 由于最近兩個多月,筆者正和小伙伴們忙于對公司新項目的開發(fā)如失,筆者主要負(fù)責(zé)項目整體架構(gòu)的搭建以及功能模塊的分工绊诲。...
    CoderMikeHe閱讀 27,032評論 74 271
  • 用到的組件 1、通過CocoaPods安裝 2褪贵、第三方類庫安裝 3掂之、第三方服務(wù) 友盟社會化分享組件 友盟用戶反饋 ...
    SunnyLeong閱讀 14,618評論 1 180