检查从Swift中的UILocalNotification启动

bit*_*ops 6 uilocalnotification swift

这是如何在Swift中检查launchOptions的后续问题 - 我的应用程序成功启动而没有崩溃,但我似乎无法正确检测应用程序何时从通知启动到正常启动.

我正在创建我的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)
Run Code Online (Sandbox Code Playgroud)

这是我从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
}
Run Code Online (Sandbox Code Playgroud)

如果我的应用程序是后台运行的,我点击警告框,我想要触发的操作正确执行,所以我知道我至少可以使一条路径正常工作.这里的问题是完全从通知启动应用程序.

小智 3

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

在这种情况下,您应该在“application didReceiveLocalNotification”方法中完成您的工作。

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
        // do your jobs here
}
Run Code Online (Sandbox Code Playgroud)