基于时间触发UIAlertViewController

时间:2018-04-25 18:11:38

标签: ios nsuserdefaults uialertview

我有UITable来展示不同的动物。在表中选择单元格时,将推送具有大UIImage的新视图控制器。目前,当您放大图像时,会触发UIAlertView,询问用户是否要下载高分辨率图像。如果他们点击是,那么" hi-res-flag"被设置为"是"在用户默认值中,他们不再看到弹出窗口。但是,如果他们选择“否”,则每次放大照片时,高分辨率标志将继续弹出。

相反,如果他们回答否,我希望这个标志偶尔会弹出 。不是每次单击物种表中的单元格,也不是每次打开应用程序时。更像是一个月一次或两次。有没有办法在iOS应用程序的逻辑中使用时间?例如,擦除为" high-res-flag"设置的值。 (如果用户默认值已经等于' no'),每月一次?

2 个答案:

答案 0 :(得分:5)

在用户首选项中存储最后显示警报的时间,然后每次在显示警报之前检查该值是否已经过了一段时间。

答案 1 :(得分:1)

TheEye的答案是一个很好的解决方案。我只想在代码方面贡献该方法。随意使用它来获取时间检查功能。因为它是斯威夫特,请忍受它。您也可以从Objective-C代码中使用它。您可以 find this code in gist here

解决方案

下面,您使用viewWillAppear委托方法查看hiResFlag是否存在。如果它存在且false,则检查是否可以显示弹出窗口:

class ImageViewController: UIViewController {

    //Whenever you enter the Image View Controller, you check whether to show popup or not
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        if let hiResFlag = hiResFlag {
            if hiResFlag == false {
                if PopUpTimeChecker.shouldShowPopUp() {
                    self.presentAlert()
                }
            }
        }
    }

    func presentAlert() {
        let alert = UIAlertController.init(title: nil, message: "Show Pop up", preferredStyle: .alert)
        let action = UIAlertAction.init(title: "Yeahh!", style: .default, handler: nil)

        alert.addAction(action)

        self.present(alert, animated: true, completion: nil)
    }

}

以下代码实现了时间检查算法。修改下面的popUpTimeInterval以设置最短时间。现在,它被设置为15天(以秒为单位)。每隔15天,当您调用shouldShowPopUp方法时,系统会显示弹出窗口。

import UIKit

//Below 4 variables, I have made them Global. No need to make them global in your case

var popUpTimeInterval: UInt64 = 1296000 //15 days in seconds

var hiResFlag: Bool? {
    get {
        return UserDefaults.standard.object(forKey: "HiResFlag") as? Bool
    }
    set {
        UserDefaults.standard.setValue(newValue, forKey: "HiResFlag")
    }
}

var isFirstTimePopUp: Bool {
    get {
        let value = UserDefaults.standard.object(forKey: "IsFirstTimePopUp")
        return value == nil ? true : value as! Bool
    }
    set {
        UserDefaults.standard.setValue(newValue, forKey: "IsFirstTimePopUp")
    }
}

var lastDateOfPopUp: Date? {
    get {
        return UserDefaults.standard.object(forKey: "LastDateOfPopUp") as? Date
    }
    set {
        UserDefaults.standard.setValue(newValue, forKey: "LastDateOfPopUp")
    }
}


class PopUpTimeChecker {

    static fileprivate func setLastPopUpDate() {
        //Setting current date to last shown pop up date
        lastDateOfPopUp = Date()
    }

    static fileprivate func timeIntervalSinceLastPopUp() -> UInt64 {
        //Returning how much time (in seconds) has passed from last popup date until now
        return UInt64(Date().timeIntervalSince(lastDateOfPopUp!))
    }

    static func shouldShowPopUp() -> Bool {
        //We proceed further only if we have the last date when pop up was displayed, else we create and set it as the current date
        if let _ = lastDateOfPopUp {
            let timeInterval = timeIntervalSinceLastPopUp()
            if timeInterval > popUpTimeInterval {
                self.setLastPopUpDate()
                return true //Show pop up
            } else {
                if isFirstTimePopUp {
                    //If this is the first time, you just allow the pop up to show, don't allow otherwise
                    isFirstTimePopUp = false
                    return true
                } else {
                    return false
                }
            }
        } else {
            self.setLastPopUpDate() //Since we don't have a last date, we set it here for starting off
            return self.shouldShowPopUp() //Recursively call method
        }
    }
}