根据时间触发 UIAlertViewController

Trigger UIAlertViewController Based on Time

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

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

将您上次显示警报的时间存储在用户首选项中,然后在每次显示警报之前检查该值是否已过特定时间。

TheEye 的回答是一个很好的解决方案。我只是想在代码方面贡献那个方法。随意使用它来获得时间检查功能。因为是Swift所以请多多包涵。您也可以在 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 天(以秒为单位)。当您调用 shouldShowPopUp 方法时,每 15 天将显示一次弹出窗口。

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
        }
    }
}