简体   繁体   English

从推送通知 IOS 打开应用程序时打开特定的 ViewController

[英]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:

MyStoryBoard我的故事板

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 中的启动选项键UIApplicationLaunchOptionsRemoteNotificationKeyUIApplicationLaunchOptionsLocalNotificationKey

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.

相关问题 iOS点击推送通知打开具体的viewcontroller - iOS click on push notification open concrete viewcontroller 通过推送通知打开应用的特定页面 - Open Specific Page of App from Push Notification 当用户点击iOS Swift的推送通知时,在特定视图中打开应用程序 - Open app in specific view when user taps on push notification with iOS Swift 收到推送通知时,IOS Swift- App无法打开特定的选定选项卡栏控制器索引 - IOS Swift- App does not open specific selected tab bar controller index when push notification is received 当用户点击带有 iOS 13 Swift 5 的推送通知时,在特定视图中打开应用程序 - Open app in specific view when user taps on push notification with iOS 13 Swift 5 应用程序无法在推送通知时打开,当它在后台时(iOS7) - App won't open on push notification, when it was in background (iOS7) 单击推送通知时如何打开应用程序的特定页面 - How to open a specific page of the app when I click on a push notification iOS推送通知打开其他应用 - iOS Push Notification open different app 在打开应用程序时未显示ios 9推送通知 - ios 9 push notification not showing while app is open 通过“推送通知”从“终止”状态打开应用程序时,iOS Web View App崩溃 - iOS Web View App crashed when Application is open from kill state through Push Notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM