当应用未运行状态iOS

时间:2016-05-24 13:17:28

标签: ios

我有tabbar控制器有三个选项卡,附加三个导航控制器我想转到导航控制器的第二个控制器就像whatsapp。我成功处理了后台状态,但没有运行状态。下面是我在ufinpplication的didfinishlaunch委托方法中的代码。

if (launchOptions != nil) {
        // Launched from push notification

        NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];


        [self performSelector:@selector(notificationObserverAfterDelay:) withObject:notification afterDelay:2.0];

    }

-(void)notificationObserverAfterDelay:(NSDictionary *)userInfo
{
  [[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshUIChatNotRunningState" object:self userInfo:userInfo];
}

1 个答案:

答案 0 :(得分:0)

Nazish Ali,

延迟2秒后发出通知确实不是一个好主意。

声明一个方法,该方法在具有标签栏的viewController中接受您的userInfo字典作为参数。

我们称之为,

YourViewController.h

-(void)appStartedFromPushNotification : (NSDictionary *)userInfo;

YourViewController.m

-(void)appStartedFromPushNotification : (NSDictionary *)userInfo {
       //now the control has reached yourViewController with tab bar
       //change the tab bar selection and perform segue and load the next viewController and use prepareforSgue to pass the data
}

现在为什么YourViewController只是为什么不能反正需要加载的实际ViewController?你不想手动弄乱导航堆栈的原因。因为YourViewController是UINavigationController的子节点,UINavigationController是你的rootView控制器,所以你自动加载YourViewController。所以只需访问它并要求它执行选项卡更改并加载viewControllers,这样您的导航堆栈将继续保持稳定:)

终于在Appdelegate,

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
 NSDictionary *userInfo = [launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (launchOptions) {
        [self handleReceiveRemoteNotification: userInfo];
    }
    return YES;
}

在AppDelegate.m中声明一个方法并访问YourViewController实例并将数据移交给它全部:)

- (void) handleReceiveRemoteNotification:(NSDictionary *)userInfo{
    UINavigationController *rootViewController = (UINavigationController *)self.window.rootViewController;
    for(UIViewController *viewController in rootViewController.viewControllers){

        if([viewController isKindOfClass:[YourViewController class]]) {
                [viewController appStartedFromPushNotification:userInfo];
        }
}

那应该做的工作:)快乐编码:)