[英]Open specific ViewController when open app from push notification IOS
I need to open a specific view controller from a push notification;我需要从推送通知中打开一个特定的视图控制器; This is my mainStoriboard:这是我的 mainStoriboard:
I was able to arrived in a ViewController but when i'm there i can't see the tabbar and the navigation controller.我能够到达 ViewController 但是当我在那里时我看不到标签栏和导航控制器。 here the code:这里的代码:
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (launchOptions != nil)
{
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main"
bundle:nil];
UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"Table1"];
self.window.rootViewController = vc;
}
return YES;
}
Can anyone help me ??谁能帮我 ??
I think you want to "NAVIGATE" to the specific ViewController upon clicking on notification.我认为您想在单击通知时“导航”到特定的 ViewController。 If that's right then You need to make your way from the root navigation controller to the destination view controller, for that you may want to push that final view controller on the current navigation stack of the application after checking if it's already on the stack or not, and if it's already there you just need to update it, otherwise push that ViewController.如果是这样,那么您需要从根导航控制器到目标视图控制器,为此您可能希望在检查它是否已经在堆栈上之后将最终视图控制器推送到应用程序的当前导航堆栈上,如果它已经存在,您只需要更新它,否则推送该 ViewController。
Currently your code just seems to make the final view controller the root view controller, that's why you are not seeing your navigation bar/tab bar.目前,您的代码似乎只是使最终视图控制器成为根视图控制器,这就是为什么您看不到导航栏/标签栏的原因。
If you need assistance with code please let me know I'll update my answer.如果您需要代码方面的帮助,请告诉我我会更新我的答案。 Good Luck祝你好运
If you mean - another tab should be selected, you can use something like this:如果您的意思是 - 应该选择另一个选项卡,您可以使用以下内容:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (launchOptions)
{
UITabBarController* controller = (UITabBarController*)self.window.rootViewController;
if ([controller isKindOfClass:[UITabBarController class]])
{
[controller setSelectedIndex:1]; // Here you should specify correct tab's number
}
}
return YES;
}
Otherwise - could you describe your problem more?否则 - 你能更详细地描述你的问题吗?
In App delegate you can put this under didReceiveLocalNotification or didRecieveRemoteNotification methods if your tab bar is the initial view controller or traverse accordingly to the tab bar controller.在 App 委托中,如果您的标签栏是初始视图控制器或相应地遍历标签栏控制器,您可以将其放在 didReceiveLocalNotification 或 didRecieveRemoteNotification 方法下。
MenuTabBarViewController *controller = (MenuTabBarViewController *)self.window.rootViewController;
controller.selectedViewController = [controller.viewControllers objectAtIndex:1]; // The tab you want to show.
Check for the launch option keys UIApplicationLaunchOptionsRemoteNotificationKey
and UIApplicationLaunchOptionsLocalNotificationKey
in the launchOptions-Dictionary检查 launchOptions-Dictionary 中的启动选项键UIApplicationLaunchOptionsRemoteNotificationKey
和UIApplicationLaunchOptionsLocalNotificationKey
Then get detail from notification userinfo,and get currentViewController with rootViewController.然后从通知用户信息中获取详细信息,并使用 rootViewController 获取 currentViewController。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = ...//init viewController in storyboard or coed;
UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
if (notification){
// do something with notification userinfo
UIViewController *rootController = self.window.rootViewController;
UIViewController *currentController = rootController;
if ([currentController isKindOfClass:[UITabBarController class]]) {
currentController = [(UITabBarController *)currentController selectedViewController];
}
if ([currentController isKindOfClass:[UINavigationController class]]){
[(UINavigationController *)currentController pushViewController:newVC animated:YES];
}else if ([currentController isKindOfClass:[UIViewController class]]){
[currentController.navigationController pushViewController:newVC animated:YES];
}
}
}
if your app just in the background, use - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
or - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
instead.如果您的应用程序只是在后台,请改用- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
或- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.