ios - 代码 : why launchOptions in didFinishLaunchingWithOptions always nil?

标签 ios xcode notifications push-notification startup

我希望我的应用程序在通过单击通知启动应用程序时执行特定操作。我想在应用程序已经进入后台运行时执行这些特定操作,但也希望在通过单击通知从头开始(未运行到后台)启动应用程序时执行这些特定操作。

当应用程序通过点击通知从后台启动时,我通过以下方式收到通知:

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification

=> 没问题!

当通过点击通知从头开始应用程序时,我想通过以下方式获取通知:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

     UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

}

但是 launchOptions 总是 nil !!当应用程序通过点击应用程序图标(正常)从头开始时为零,当应用程序通过点击通知从头开始(不正常)时也为零。

有人知道如何解决这个问题吗?

谢谢!!!

编辑 1

以下是我的通知是如何创建的(Joe 问题):

  NSDictionary *userInfo = [NSDictionary dictionaryWithObjects:
[NSArray arrayWithObjects:notifText,latitudeString,longitudeString,nil]
forKeys:[NSArray arrayWithObjects:@"notifText",@"latitude",@"longitude", nil]];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = itemDate;
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody =msg;
    localNotif.alertAction = @"Ok";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.userInfo = userInfo;

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
    [localNotif release];

编辑 2(回答我的问题!:))

这是我用来调试我的应用程序的过程,但是...这个过程是错误的!!

  1. 我使用“编辑方案”菜单中的“等待 MyApp.app 启动”选项设置我的调试器
  2. 我第一次使用 XCode 启动了我的应用程序(从头开始启动)XCode 显示“正在等待我的应用程序启动”=> 我点击了我的应用程序图标以启动该应用程序
  3. 应用程序已启动 => 我点击了主页按钮 => 显示了通知
  4. 我单击了 XCode 中的停止按钮以关闭应用程序 我使用 XCode 重新启动它 => XCode 再次显示“等待我的 MyApp 启动”消息 => 我单击了 状态栏中的通知以启动应用程序
  5. => launchOptions 为零!

launchOptions 等于 nil 是因为使用 XCode 重新启动应用程序(在本例中使用“等待我的 MyApp 启动”选项)会删除通知,即使它仍然显示在状态栏中......

为了能够在通过单击通知从头开始重新启动应用程序后调试检查 launchOptions 的内容,似乎唯一的方法是在 UIAlert 中显示此内容,如 Tammo 的回答中所述免费。因此,在这种特定情况下使用以下内容进行调试:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    return YES;
}

感谢大家的帮助!!!!

最佳答案

我在您分享的代码中找不到错误。通常这应该在模拟器和设备上都有效。这是一个对我有用的示例:首先生成一个新的单 View iPhone 应用程序(打开 ARC 和 Storyboard)。然后修改AppDelegate中的两个方法如下:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"options" message:[launchOptions[UIApplicationLaunchOptionsLocalNotificationKey] description] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    return YES;
}

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    [[UIApplication sharedApplication] cancelAllLocalNotifications];

    UILocalNotification *localNotif = [[UILocalNotification alloc] init];
    localNotif.fireDate = [NSDate dateWithTimeInterval:10.0 sinceDate:[NSDate date]];
    localNotif.timeZone = [NSTimeZone defaultTimeZone];
    localNotif.alertBody = @"Just some text";
    localNotif.alertAction = @"OK";
    localNotif.soundName = UILocalNotificationDefaultSoundName;
    localNotif.applicationIconBadgeNumber = 1;
    localNotif.userInfo = @{@"test": @YES};

    [[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
}

然后启动这个应用程序,按主页按钮,然后停止应用程序。如果您点击在按下主页按钮 10 秒后出现的本地通知,您应该看到类似这样的内容,这表明本地通知已传递给 -application:didFinishLaunchingWithOptions::

screenshot of the iPhone Simulator showing an alert view that displays a local notification

我的建议是:首先使用我在上面发布的示例来确保您的设置没有出现任何问题,然后检查您在代码中所做的不同之处。

编辑

这适用于问题的编辑 2:在等待应用程序启动时(在模拟器中,而不是在设备上),本地通知似乎也对我有用。试试这个:

  1. 安装上述示例应用并在禁用“等待 MyApp.app 启动”的情况下启动它。
  2. 点击主页按钮,然后通过 Xcode 或任务栏停止应用。
  3. 启用“等待 MyApp.app 启动”。
  4. 如果您现在点击通知中心的通知,它会显示在提醒 View 中。

关于ios - 代码 : why launchOptions in didFinishLaunchingWithOptions always nil?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13396630/

相关文章:

ios - 在自定义 UICell 中获取 UIImage 的框架在 iOS8.3 中使用自动布局

ios - 在字符串中添加空格

ios - 苹果商店如何确定应用程序的语言信息

iphone - 在 Xcode 中,我是否在 build 或 version 下输入应用程序的版本号

python - 要求另一个线程做某事

iOS 8.1 杀死了我的 WebView 的一部分。把招工广告

ios - 删除应用程序后,我创建的内存路径和其中的数据会发生什么变化?

ios - 如何在 iOS 中录制声音?

ios - 如何确保 iOS 本地通知在*应用程序终止后*得到传递

java - 有没有办法更改内置 controlfx 通知弹出窗口的颜色?