繁体   English   中英

当应用程序处于后台时,在Swift 2.0 Xcode 7中显示警报消息

[英]Show alert message in Swift 2.0 Xcode 7 when the app is in background

我目前正在尝试将警报消息显示为我的应用程序中的弹出窗口。 当前在前台运行的应用程序在屏幕上时,它会显示弹出窗口。 但是,问题在于,当应用程序在后台运行时,弹出窗口不会显示在iOS上。 仅在用户重新输入应用程序时显示。

这是弹出源代码:

func popupDeAlerta(){
    // Dismiss Message
    let alertController = UIAlertController(title: "Alert title", message:
        "alert message", preferredStyle: UIAlertControllerStyle.Alert) 

    AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate)) // vibration

    alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)
}

是的,这是正确的行为,“警报”视图控制器仅在添加了该控制器的控制器上显示警报,例如:

alertController.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default,handler: nil))
    self.presentViewController(alertController, animated: true, completion: nil)

如果您想要一个会在一段时间后自动提醒您的操作,则必须在您的应用中实现通知。

通知链接在这里

到目前为止,我发现的最好方法。 如果有人有其他建议或可以进一步讨论,则非常欢迎。 在Appdelegate.swif上有一个预定义的函数,称为“ applicationDidEnterBackground”。 只是这样做:

func applicationDidEnterBackground(application: UIApplication) {

let notification = UILocalNotification() 
notification.alertAction = "Title Message" 
notification.alertBody = "Body Message" 
notification.fireDate = NSDate(timeIntervalSinceNow: 5) 
notification.soundName = UILocalNotificationDefaultSoundName 
notification.category = "INVITE_CATEGORY"; 
UIApplication.sharedApplication().scheduleLocalNotification(notification) }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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