Ios 在AppDelegate中重定向带有推送通知的视图
我的目标是编写一个代码,当用户收到推送通知时,我希望该用户被重定向到另一个视图。 如果用户带有推送通知,并且他第一次查看控制器(欢迎主屏幕等)(但未登录)) 这两行代码正在工作,但是,如果用户已经在另一个视图控制器(登录/登录/用户页面等)中,这段代码将无法工作并重定向。 我什么都试过了,但还是找不到解决办法。我的最终目标是:Ios 在AppDelegate中重定向带有推送通知的视图,ios,swift,cocoa-touch,push-notification,uistoryboardsegue,Ios,Swift,Cocoa Touch,Push Notification,Uistoryboardsegue,我的目标是编写一个代码,当用户收到推送通知时,我希望该用户被重定向到另一个视图。 如果用户带有推送通知,并且他第一次查看控制器(欢迎主屏幕等)(但未登录)) 这两行代码正在工作,但是,如果用户已经在另一个视图控制器(登录/登录/用户页面等)中,这段代码将无法工作并重定向。 我什么都试过了,但还是找不到解决办法。我的最终目标是: if let rootViewController = self.window!.rootViewController as? ViewController {
if let rootViewController = self.window!.rootViewController as? ViewController
{
var rootView: UserViewController = UserViewController()
if let window = self.window{
window.rootViewController = rootView
}
rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)
println(self.window?.rootViewController)
}
有人能给我一个主意吗?答案代码是Objective-C,我想谈谈逻辑。为此,在创建推送通知时,必须设置userInfo值,以便在推送通知中传递数据 使用此委托方法:
application:(UIApplication*)application-didReceiveMemoteNotification:(NSDictionary*)userInfo-fetchCompletionHandler:(void(^)(UIBackgroundFetchResult))completionHandler
您可以通过处理userInfo
NSDictionary *segueDictionary = [userInfo valueForKey:@"aps"];
NSString *segueName=[[NSString alloc]initWithFormat:@"%@",[segueDictionary valueForKey:@"segueName"]];
if([segueName isEqualToString:@"hospitalSegue"])
{
// implement your code to redirect
}
答案代码为Objective-C,您可以使用以下方法从app delegate获取prentviewcontroller我使用以下代码解决此问题:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[self topViewController]presentViewController:nav animated:YES completion:nil];
}
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
听起来是个好主意。您如何接收推送通知?也许您可以在InterfaceBuilder中为我们概述您的视图,这取决于您的层次结构,以及如何重定向到不同的视图。您是否以模式显示
UIViewController
?请使用NSNotificationCenter。应用程序内委托(DidReceiveEmoteNotification),发出本地通知,并在需要时显示另一个ViewController,方法是注册到本地通知,并检查屏幕上是否显示该视图控制器。@jeraldo谢谢,我与nsnotificationcenter解决了这个问题!
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[self topViewController]presentViewController:nav animated:YES completion:nil];
}
- (UIViewController*)topViewController {
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
- (UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController {
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController* tabBarController = (UITabBarController*)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}