使用 UIApplication DidEnterBackgroundNotification 标志

Using UIApplicationDidEnterBackgroundNotification flag

我使用 NSTimer 调用下面的函数,我希望在倒计时结束时触发声音。我设法使用下面的代码在活动模式和后台进行。现在我想将它们包装在一个 if 语句中,这取决于应用程序状态,但它总是触发 else 部分。

func CountDown() {

    Timer  -= 1

    CountDownLabel.text = MakeTimeLabel()
    ProgressCircle.strokeEnd = 1-(CGFloat(Timer/Test))

    if Timer == -1 {

        if UIApplicationDidEnterBackgroundNotification == true {

        let soundnotification = UILocalNotification()

        soundnotification.soundName = "Boxing.wav"

        UIApplication.sharedApplication().scheduleLocalNotification(soundnotification)

        println("timer is done in background mode")

        } else {

        // Load Sound
        soundlocation = NSBundle.mainBundle().URLForResource("Boxing", withExtension: "wav")!

        player = AVAudioPlayer(contentsOfURL: soundlocation, error: &Error)
        player.volume = 1 

        // Play Sound
        player.play()

        println("timer is done in active mode")

        }

        StopButton(self)

    }   
}

提前致谢

这是因为您正在比较字符串 "UIApplicationDidEnterBackgroundNotification" 和布尔值 "true"。这将始终被评估 "false".

我相信你需要比较一下:

UIApplication.sharedApplication().applicationState == UIApplicationState.Background