繁体   English   中英

检查iOS应用是否从本地通知启动?

[英]Checking if iOS app was launched from local notification?

如何检测是否从本地通知中启动了未处于活动,非活动或后台状态(已终止)的应用程序? 到目前为止,我已经在App Delegate的didFinishLaunchingWithOptions中尝试了两种方法:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // METHOD 1:
    if let options = launchOptions {
        if let key = options[UIApplicationLaunchOptionsLocalNotificationKey] {
            notificationCenter.postNotification(NSNotification(name: "applicationLaunchedFromNotification", object: nil))
        }
    }

    // METHOD 2:
    let notification = launchOptions?[UIApplicationLaunchOptionsLocalNotificationKey] as! UILocalNotification!
    if (notification != nil) {
        notificationCenter.postNotification(NSNotification(name: "applicationLaunchedFromNotification", object: nil))
    }

    return true
}

在我的View Controller中,我观察ViewDidLoad中的通知,并作为响应设置UILabel的文本:

override func viewDidLoad() {
    super.viewDidLoad()
    notificationCenter.addObserver(self, selector: "handleAppLaunchFromNotification", name: "applicationLaunchedFromNotification", object: nil)
}

func handleAppLaunchFromNotification() {
    debugLabel.text = "app launched from notification"
}

但是从本地通知启动终止的应用程序后,再也不会设置UILabel的文本。

我的问题是:

  1. 我究竟做错了什么?
  2. 除了设置UILabel之外,还有没有比这种情况更简单的调试方法了? 应用终止后,Xcode的调试器将分离该应用,并且我无法使用print()

您在didFinishLaunchingWithOptions中检查本地通知,此方法仅包含远程通知的launchOptions 如果应用程序处于终止状态,并且您对本地通知执行操作,则didFinishLaunchingWithOptions方法之后, didReceiveLocalNotification将被调用。

暂无
暂无

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

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