检查是否从 Swift 中的 UILocalNotification 启动 [英] Check for launching from UILocalNotification in Swift

查看:24
本文介绍了检查是否从 Swift 中的 UILocalNotification 启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

This is a follow-up question to How to check launchOptions in Swift? - I got my app to launch successfully without crashing, but I can't seem to correctly detect when the app is launching from a notification vs a normal launch.

我正在像这样创建我的 UILocalNotification:

I am creating my UILocalNotification like so:

// 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 启动应用程序时尝试处理的代码.

And this is my code for trying to handle when the app is launched from a 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
}

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

If my app is backgrounded and I tap on the alert box, the action I want to fire is executed correctly, so I know that I at least can get one path working. The issue here is launching the application completely from a notification.

推荐答案

如果您的 App 在后台运行,则不会调用application didFinishLaunching"方法.它已经推出.

If your App is running in background, the "application didFinishLaunching" method will not be called. It has already been launched.

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

In this case you should do your job in the "application didReceiveLocalNotification" method.

func application(application: UIApplication!, didReceiveLocalNotification notification: UILocalNotification!) {
        // do your jobs here
}

这篇关于检查是否从 Swift 中的 UILocalNotification 启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆