gpt4 book ai didi

ios - 如何在推送通知中导航到另一个 View Controller

转载 作者:行者123 更新时间:2023-11-29 02:04:21 25 4
gpt4 key购买 nike

我正在做一个应用程序。其中我有一些 View Controller ,例如 A->B、A->C、A->D 和 B->E、C->F、D->G 和 E->H,F->I 和 G->J。所以我在 E View Controller 中,每当有通知时,我必须移动到 G View Controller 。如果我在除 G 之外的任何 View Controller 中,我需要移动到G View Controller 。所以请告诉我该怎么做。

最佳答案

您需要先在 AppDelegate.m 中设置一些代码以在应用打开时响应推送:

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//If opening app from notification
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];

}
//If app is already open
else {


[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];

}


}

如果应用程序通过推送打开(即您从锁定屏幕上滑动),我们会在此处触发 NSNotification,如果应用程序已经打开,还会有不同的通知。您不必使用两种不同类型的 NSNotification,但它可能对您有用。我不太确定您的 View Controller 设置是什么,但假设您对根 Controller 中的所有内容都使用 UINavigation Controller ,您只需将其设置为在 ViewDidLoad 中收听即:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appOpenPush) name:@"appOpenPush" object:nil];

在你调用的方法中是这样的:

-(void)appOpenPush {

NSLog(@"got a push while app was open");
//Get the view controller

UIViewController *lastViewController = [[self.navigationController viewControllers] lastObject];
if([lastViewController isKindOfClass:[MyViewController class]]) {

//do your segue here

}

else if//////do this for all your cases...

}

这里我们检查 View Controller 是什么类型的类,然后选择合适的转场。

希望你至少做了一点工作并理解我写的东西..

关于ios - 如何在推送通知中导航到另一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29963874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com