这是
How to check launchOptions in Swift?的后续问题 – 我的应用程序在没有崩溃的情况下成功启动,但我似乎无法正确检测应用程序何时从通知启动到正常启动.
我正在创建我的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”方法.它已经推出.
在这种情况下,您应该在“application didReceivelocalnotification”方法中完成工作.
func application(application: UIApplication!,didReceivelocalnotification notification: UIlocalnotification!) { // do your jobs here }