简体   繁体   中英

How to do timeIntervalSinceNow in Swift3?

I was making a notification and watching a tutorial on it and when I typed in:

notification.fireDate = NSDate(timeIntervalSinceNow: 0)

it says

Argument labels '(timeIntervalSinceNow:)' do not match any available overloads

How do I fix this? Here is my code:

import UIKit
import UserNotifications

class ViewController: UIViewController {
    var timer = Timer()
    var time = 10
    override func viewDidLoad() {
        super.viewDidLoad()
        timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: Selector(("Notification")), userInfo: nil, repeats: true)
        // Do any additional setup after loading the view, typically from a nib.
    }
    func notification() {
        time -= 1
        if time <= 0 {
            let notification = UILocalNotification()
            notification.alertAction = "Call"
            notification.alertBody = "You have a call right now"
            notification.fireDate = NSDate(timeIntervalSinceNow: 0)
            UIApplication.shared.scheduleLocalNotification(notification)
            timer.invalidate()
        }
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    @IBAction func pushNotification(_ sender: AnyObject) {
        let AlertView = UIAlertController(title: "Time for your call!", message: "Press go to continue", preferredStyle:  .alert)
        AlertView.addAction(UIAlertAction(title: "Go", style: .default, handler: nil))
        self.present(AlertView, animated: true, completion: nil)
    }
}

Swift 3

let minute:TimeInterval = 11.0 * 60.0;
Date(timeIntervalSinceNow: minute);

for +11 minutes to now.

If you want the fire date to be now, it's just:

notification.fireDate = Date()

As Leo noted elsewhere, the selector is incorrect. It should be:

weak var timer: Timer?
var time = 10

override func viewDidLoad() {
    super.viewDidLoad()

    timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(handleTimer(_:)), userInfo: nil, repeats: true)
}

func handleTimer(_ timer: Timer) {
    time -= 1

    if time <= 0 {
        let notification = UILocalNotification()

        notification.alertAction = "Call"
        notification.alertBody = "You have a call right now"
        notification.fireDate = Date()

        UIApplication.shared.scheduleLocalNotification(notification)

        timer.invalidate()
    }
}

You then asked:

that definitely works but now I don't get a notification when I leave the app

You don't get the notification when you leave your app, because the Timer doesn't continue to fire when the app isn't running. It's better to eliminate the timer altogether and just immediately schedule the local notification for the desired time. For example, to schedule a local notification to fire in 10 seconds:

override func viewDidLoad() {
    super.viewDidLoad()

    scheduleLocalNotification(delay: 10)
}

func scheduleLocalNotification(delay: TimeInterval) {
    let notification = UILocalNotification()

    notification.alertAction = "Call"
    notification.alertBody = "You have a call right now"
    notification.fireDate = Date().addingTimeInterval(delay)

    UIApplication.shared.scheduleLocalNotification(notification)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM