2016-09-21 4 views
1

ich eine Meldung zu machen und ein Tutorial auf sie zu beobachten und, wenn ich in getippt:Wie zeitIntervalSinceNow in Swift3 zu tun?

notification.fireDate = NSDate(timeIntervalSinceNow: 0) 

es sagt

Argument Labels '(timeIntervalSinceNow :)' passen nicht alle verfügbaren Überlastungen

Wie behebe ich das? Hier ist mein 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) 
    } 
} 
+0

Aber dann heißt es, ich muss es machen "notification.fireDate = NSDate() als Date" und dann funktioniert es nicht –

+0

@LeoDabus Ich bekomme keinen Fehler, wenn ich das mache, was du gesagt hast, aber ich bekomme Thread 1: Signal SIGABRT –

+0

@LeoDabus wo soll ich das hinstellen? –

Antwort

1

Wenn Sie das Feuer Datum jetzt sein wollen, es ist nur:

notification.fireDate = Date() 

Als Leo an anderer Stelle erwähnt, ist der Wähler nicht korrekt ist. Es sollte sein:

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

Sie dann gefragt:

das funktioniert auf jeden Fall, aber jetzt verstehe ich nicht eine Benachrichtigung, wenn ich die app

Sie verlassen nicht das bekommen Benachrichtigung, wenn Sie Ihre App verlassen, weil die Timer nicht weiter feuert, wenn die App nicht ausgeführt wird. Es ist besser, den Timer komplett zu eliminieren und sofort die lokale Benachrichtigung für die gewünschte Zeit zu planen. Um zum Beispiel eine lokale Benachrichtigung planen innerhalb von 10 Sekunden zu schießen:

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) 
} 
+0

@LeoDabus sagte schon, aber wenn ich das eintippe bekomme ich ein 'Thread 1: Signal SIGABRT' –

+2

Ja, das liegt daran das dein Selektor falsch ist, wie Leo darauf hingewiesen hat. – Rob

1

Swift 3

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

für 11 Minuten jetzt.