XCode:为什么 didFinishLaunchingWithOptions 中的 launchOptions 总是为零? [英] XCode : why launchOptions in didFinishLaunchingWithOptions always nil?

查看:24
本文介绍了XCode:为什么 didFinishLaunchingWithOptions 中的 launchOptions 总是为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的应用在通过点击通知启动应用时执行特定操作.我想在应用程序已经在后台运行时执行这些特定的操作,但也希望在通过单击通知从 SCRATCH 启动应用程序(而不是运行到后台)时执行这些特定的操作.

I want my app to do specific things when the app is launched by a click on a notification. I want to do these specific things when the app is already running into background BUT ALSO when the app is started FROM SCRATCH (not running into background) by a click on the notification.

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

When the app is started from background by a click on the notification, I get the notification via:

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

=> 没问题!

当应用通过点击通知从头开始启动时,我想通过以下方式获得通知:

When the app is started from scratch by a click on the notification, I would like to get the notification via:

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

     UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

}

但是 launchOptions 总是 nil !!当应用程序通过点击应用程序图标从头开始启动时(正常)以及当应用程序通过点击通知从头开始启动时(不正常),它为零.

But launchOptions is always nil !! It is nil when the app is started from scratch via a click on the app icon (normal) but also when the app is started from scratch via a click on a notification (not normal).

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

Anybody knows how to solve this issue ?

谢谢!!!

编辑 1

以下是我的通知的创建方式(Joe 问题):

Here is how my notifications are created (Joe question):

  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(回答我的问题!:))

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

Here is the procedure I used to debug my app but...this procedure is wrong!!

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

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

launchOptions equal to nil is due to the fact that relaunching the app with XCode (in this case with the "Waiting for my MyApp to launch" option) deletes the notifications even if it is still displayed in the status bar...

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

To be able to debug check what is the content of launchOptions after a relaunch of the app from scratch by a click on a notification, it seems that the only way is to display this content in a UIAlert as mentioned in answer by Tammo Freese. So, use the following to debug in this specific case:

- (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;
}

感谢大家的帮助!!!!

Thanks all for your help !!!!

推荐答案

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

I could not find an error in the code you shared. Normally this should work both in the simulator, and on the device. Here is an example that worked for me: First generate a new Single View iPhone app (ARC and Storyboards on). Then change two methods in the AppDelegate as follows:

- (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::

Then start this app, press the home button, then stop the app. If you tap on the local notification which comes in 10 seconds after pressing the home button, you should see something like this, which shows the local notification has been passed to -application:didFinishLaunchingWithOptions::

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

My advice is: First get the example I posted above to work to make sure nothing has gone awry with your setup, then check what you are doing differently in your code.

编辑

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

This applies to Edit 2 of the question: Local notifications also seem to work for me when waiting for the app to launch (in the simulator, not on the device). Try this:

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

这篇关于XCode:为什么 didFinishLaunchingWithOptions 中的 launchOptions 总是为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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