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

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

问题描述

我希望我的应用在通过点击通知启动应用时执行特定的操作。当应用程序已经运行到后台时,我想做这些特定的事情但是当通过点击通知启动应用程序FROM 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总是零!当应用程序通过点击应用程序图标(正常)从头开始,但是当通过点击通知(不正常)从头开始启动应用程序时,它是零。

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启动=>我点击我的APP ICON启动应用程序

  3. 应用程序启动=>我点击主页按钮= >显示通知

  4. 我点击XCode中的停止按钮关闭应用程序我用XCode重新启动它=> XCode再次显示等待我的MyApp启动消息=>我点击了状态栏中的
    通知以启动应用

  5. => launchOptions为零!

  1. I set up my debugger with the "Wait for MyApp.app to launch" option in the "Edit Scheme" menu
  2. 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
  3. The app is launched => I clicked on the home button => the notification is displayed
  4. 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
  5. => launchOptions is nil !

launchOptions等于nil是因为重新启动应用程序与XCode(在这种情况下与Waitin g我的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,似乎唯一的方法是在Tammo Freese的回答中提到的UIAlert中显示此内容。因此,在这种特定情况下使用以下内容进行调试:

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 !!!!

推荐答案

我在您分享的代码中找不到错误。通常,这应该在模拟器和设备上都有效。这是一个适合我的示例:首先生成一个新的Single View iPhone应用程序(ARC和故事板上)。然后更改 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. 如果您现在点按通知中心的通知,它会显示在提醒视图中。

  1. Install the sample app described above and launch it with "Wait for MyApp.app to launch" disabled.
  2. Click on the home button, then stop the app via Xcode or via the task bar.
  3. Enable "Wait for MyApp.app to launch".
  4. If you now tap the notification in the notification center, it is shown in the alert view.

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

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