繁体   English   中英

XCode:为什么didFinishLaunchingWithOptions中的launchOptions总是为零?

[英]XCode : why launchOptions in didFinishLaunchingWithOptions always nil?

当点击通知启动应用程序时,我希望我的应用程序执行特定的操作。 当应用程序已经运行到后台时我想做这些特定的事情但是当通过点击通知启动应用程序FROM SCRATCH(没有运行到后台)时。

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

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

=>没有问题!

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

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

     UILocalNotification *localNotif = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];

}

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

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

谢谢 !!!

编辑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显示“等待我的MyApp启动”=>我点击我的应用程序图标启动应用程序
  3. 应用程序启动=>我点击主页按钮=>显示通知
  4. 我点击XCode中的停止按钮关闭应用程序我用XCode重新启动它=> XCode再次显示“等待我的MyApp启动”消息=>我点击状态栏中的通知启动应用程序
  5. => launchOptions是零!

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

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

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

感谢你的帮助 !!!!

我在您分享的代码中找不到错误。 通常这应该在模拟器和设备上都有效。 这是一个适合我的示例:首先生成一个新的Single View iPhone应用程序(ARC和故事板上)。 然后在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: ::

iPhone模拟器的屏幕截图,显示显示本地通知的警报视图

我的建议是:首先得到我上面发布的示例以确保您的设置没有出错,然后检查您在代码中做的不同。

编辑

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

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

不知道这是不是你想要的,但你错过了'返回YES;'? 因为我使用它来从通知执行segue到新视图,它工作正常

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UINavigationController *navigation = (UINavigationController *) self.window.rootViewController;

    [navigation.visibleViewController performSegueWithIdentifier:@"Ident" sender:nil];

    return YES;
}

我的结论和建议是否可以解决任何人,请参阅下文

每次使用新版本来测试应用程序时,都必须使用最新应用程序生成的通知测试通知单击操作。 如果您继续使用旧版本生成的旧通知测试点击操作,那么它会出现意外行为(意味着它能够以某种方式启动应用程序,但它不会在didFinishLaunchingWithOptions中返回任何有效信息:)

请原谅我,当谈到Apple的推送通知服务时,我仍然是新手,但我做了一些阅读他们的文档,我的猜测是,在创建本地通知时可能会出现导致问题的原因。

我会仔细检查你是如何与苹果的实例创建本地通知如这里清单2-1只是为了排除这种可能性。 由于在此线程中没有向我们显示用于创建本地通知的某些代码,因此很难评估本地通知的实例化是否确实正确。 它可能最终变得简单,因为火灾日期或通知中的其他内容没有正确设置。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM