检查是否从Swift中的UILocalNotification启动

检查是否从Swift中的UILocalNotification启动,swift,uilocalnotification,Swift,Uilocalnotification,这是一个后续问题-我让我的应用程序在没有崩溃的情况下成功启动,但我似乎无法正确检测应用程序是从通知启动还是正常启动 我正在创建UILocalNotification,如下所示: // set up a frequently recurring notification here just for testing... var fast = UILocalNotification() fast.fireDate = NSDate(timeIntervalSinceNow: 15) fast.ale

这是一个后续问题-我让我的应用程序在没有崩溃的情况下成功启动,但我似乎无法正确检测应用程序是从通知启动还是正常启动

我正在创建UILocalNotification,如下所示:

// set up a frequently recurring notification here just for testing...
var fast = UILocalNotification()
fast.fireDate = NSDate(timeIntervalSinceNow: 15)
fast.alertBody = "Alert Message"
fast.timeZone = NSTimeZone.localTimeZone()
fast.repeatInterval = NSCalendarUnit.CalendarUnitMinute
fast.userInfo = ["Important":"Data"]
UIApplication.sharedApplication().scheduleLocalNotification(fast)
这是我的代码,用于尝试处理从UILocalNotification启动应用程序的时间

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    if var launch = launchOptions {
        if var key = launch.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey) {
            // I never seem to reach this point...
        }
    }
    return true
}

如果我的应用程序是后台的,并且我点击了警报框,那么我想要触发的操作将正确执行,因此我知道我至少可以让一条路径工作。这里的问题是完全从通知启动应用程序。

如果您的应用程序在后台运行,则不会调用“application didFinishLaunching”方法。它已经启动了

func application(application: UIApplication!, didFinishLaunchingWithOptions launchOptions: NSDictionary!) -> Bool {
    if var launch = launchOptions {
        if var key = launch.objectForKey(UIApplicationLaunchOptionsLocalNotificationKey) {
            // I never seem to reach this point...
        }
    }
    return true
}
在这种情况下,您应该使用“application didReceiveLocalNotification”方法完成您的工作


如果直接启动应用程序,
LaunchOptions
将为零。如果通过通知而不是直接启动应用程序,则代码将运行


如果直接启动应用程序,则需要其他一些方法来解决此问题。例如,使用
本地通知的时间和当前时间,并决定显示哪个视图。

是的,该部分工作正常。然而,我说的是应用程序正在启动的情况——也就是说,它不是在后台运行的。