iOS/XCode:如何通过点击通知或跳板应用程序图标来了解该应用程序已启动?

Reg*_*_AG 7 notifications cocoa-touch push-notification ios

我想知道是否有办法知道某个应用程序(可以在后台关闭或打开)是否已通过点击启动:

  • 通知(在通知中心)?
  • 或跳板上的应用程序图标?

谢谢 !!

Cfr*_*Cfr 11

把这段代码:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];

    if (notification) {
        // launched from notification
    } else {
        // from the springboard
    }
}
Run Code Online (Sandbox Code Playgroud)

在你的UIApplicationDelegate.


Bla*_*rog 6

从Apple Docs上安排,注册和处理通知 :

iOS注意:在iOS中,您可以确定是否由于用户点击操作按钮而启动了应用程序,或者是否通过检查应用程序状态将通知传递给已在运行的应用程序.在委托的应用程序实现中:didReceiveRemoteNotification:application:didReceiveLocalNotification:方法,获取applicationState属性的值并对其进行评估.如果值为UIApplicationStateInactive,则用户点击操作按钮; 如果值为UIApplicationStateActive,则应用程序在收到通知时位于最前端.