XCode:为什么 didFinishLaunchingWithOptions 中的 launchOptions 总是为零?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13396630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XCode : why launchOptions in didFinishLaunchingWithOptions always nil?
提问by Regis_AG
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.
我希望我的应用程序在通过单击通知启动应用程序时执行特定操作。我想在应用程序已经进入后台运行时执行这些特定的操作,但也希望在通过单击通知从 SCRATCH 启动应用程序(而不是运行到后台)时执行这些特定的操作。
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
=> NO PROBLEM !
=> 没问题!
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];
}
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).
但是 launchOptions 始终为零!当应用程序通过点击应用程序图标(正常)从头开始启动时,以及通过点击通知(不正常)从头开始启动应用程序时,它是零。
Anybody knows how to solve this issue ?
有谁知道如何解决这个问题?
Thanks !!!
谢谢 !!!
EDIT 1
编辑 1
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];
EDIT 2 (ANSWER TO MY QUESTION! :))
编辑 2(回答我的问题!:))
Here is the procedure I used to debug my app but...this procedure is wrong!!
这是我用来调试我的应用程序的程序,但是……这个程序是错误的!!
- I set up my debugger with the "Wait for MyApp.app to launch" option in the "Edit Scheme" menu
- I launched my app with XCode a first time (launch from scratch) XCode displays the "Waiting for my MyApp to launch" => I CLICKED ON MY APP ICON to launch the app
- The app is launched => I clicked on the home button => the notification is displayed
- I clicked on the stop button in XCode to close the app I relaunched it with XCode => XCode displays again the "Waiting for my MyApp to launch" message => I CLICKED ON THE NOTIFICATION in the status bar to launch the app
- => launchOptions is nil !
- 我使用“编辑方案”菜单中的“等待 MyApp.app 启动”选项设置了我的调试器
- 我第一次用 XCode 启动了我的应用程序(从头启动)XCode 显示“正在等待我的 MyApp 启动”=> 我点击我的应用程序图标来启动应用程序
- 应用程序启动 => 我点击了主页按钮 => 显示通知
- 我点击 XCode 中的停止按钮关闭应用程序我用 XCode 重新启动它 => XCode 再次显示“等待我的 MyApp 启动”消息 => 我点击状态栏中的通知以启动应用程序
- => launchOptions 为零!
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 等于 nil 是因为使用 XCode 重新启动应用程序(在这种情况下使用“等待我的 MyApp 启动”选项)会删除通知,即使它仍然显示在状态栏中...
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:
为了能够通过单击通知从头重新启动应用程序后调试检查 launchOptions 的内容,似乎唯一的方法是在 UIAlert 中显示此内容,如 Tammo Freese 的回答中所述。因此,在这种特定情况下,请使用以下内容进行调试:
- (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 !!!!
感谢你的帮助 !!!!
回答by Tammo Freese
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:
我在您共享的代码中找不到错误。通常这应该在模拟器和设备上都有效。这是一个对我有用的示例:首先生成一个新的单视图 iPhone 应用程序(ARC 和 Storyboards)。然后把里面的两个方法改成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];
}
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:
:
然后启动这个应用程序,按主页按钮,然后停止应用程序。如果您点击按下主页按钮 10 秒后出现的本地通知,您应该会看到类似这样的内容,这表明本地通知已传递给-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.
我的建议是:首先使用我上面发布的示例来确保您的设置没有出现任何问题,然后检查您在代码中所做的不同之处。
Edit
编辑
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:
这适用于问题的编辑 2:在等待应用程序启动时(在模拟器中,而不是在设备上),本地通知似乎也对我有用。尝试这个:
- Install the sample app described above and launch it with "Wait for MyApp.app to launch" disabled.
- Click on the home button, then stop the app via Xcode or via the task bar.
- Enable "Wait for MyApp.app to launch".
- If you now tap the notification in the notification center, it is shown in the alert view.
- 安装上述示例应用程序并在禁用“等待 MyApp.app 启动”的情况下启动它。
- 单击主页按钮,然后通过 Xcode 或通过任务栏停止应用程序。
- 启用“等待 MyApp.app 启动”。
- 如果您现在点击通知中心中的通知,它会显示在警报视图中。
回答by iDevAmit
My conclusion and suggestion if it can hep anyone, refer below
我的结论和建议是否可以帮助任何人,请参考以下
Every time you have new build to test your app, you must test the notification click actions with the notifications generated by latest app. If you keep on testing of click actions with old notifications generated by older build then It will behave unexpectedly (means somehow its able to launch the app but it will not return you any valid info in didFinishLaunchingWithOptions:)
每次您有新版本来测试您的应用程序时,您必须使用最新应用程序生成的通知来测试通知点击操作。如果您继续使用旧版本生成的旧通知测试点击操作,那么它的行为会出乎意料(意味着它能够以某种方式启动应用程序,但它不会在 didFinishLaunchingWithOptions 中返回任何有效信息:)
回答by agierens
Dont know if this is what your looking for, but are you missing 'return YES;'? Because i use this to perform a segue to a new view from a notification and it works fine
不知道这是否是您要找的,但是您是否缺少“返回是;”?因为我用它来从通知执行到新视图的 segue 并且它工作正常
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UINavigationController *navigation = (UINavigationController *) self.window.rootViewController;
[navigation.visibleViewController performSegueWithIdentifier:@"Ident" sender:nil];
return YES;
}
回答by Anil Natha
Please forgive me, I am still new when it comes to Apple's Push Notification service, but I did some reading through their documentation and my guess is that there may be something in the creation of your local notification that is causing the problem.
请原谅我,当谈到 Apple 的推送通知服务时,我还是个新手,但我阅读了他们的文档,我猜测可能是在创建本地通知时出现了问题。
I would double check how you are creating your local notification with Apple's Example as shown herein Listing 2-1 just to rule out that possibility. Since some of the code that is used for creating your local notification isn't displayed to us in this thread, it makes it difficult to assess if the instantiation of your local notification is indeed correct. It could end up being as simple as something being wrong with the fire date or something else in the notification not being set up correctly.
我会仔细检查你是如何创建如图所示与苹果公司的例子本地通知此清单2-1只是为了排除这种可能性。由于用于创建本地通知的某些代码未在此线程中显示给我们,因此很难评估本地通知的实例化是否确实正确。最终可能会很简单,比如开火日期有问题或通知中的其他内容没有正确设置。