在 AppDelegate 中使用推送通知重定向视图
Redirect a View with Push Notification in AppDelegate
我的目标是编写一段代码,当用户收到推送通知时,我希望将该用户重定向到另一个视图。
如果用户带有推送通知并且他已经第一次查看控制器(欢迎主屏幕等(但未登录))
var rootViewController = self.window!.rootViewController as! ViewController
rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)
这几行代码可以正常工作,但是,如果用户在另一个视图控制器中(登录 in/login/user 页面等),这段代码将不起作用并重定向。
我尝试了一切,但仍然无法提出解决方案。我的最终目标是:
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 didReceiveRemoteNotification:(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;
}
}
我的目标是编写一段代码,当用户收到推送通知时,我希望将该用户重定向到另一个视图。 如果用户带有推送通知并且他已经第一次查看控制器(欢迎主屏幕等(但未登录))
var rootViewController = self.window!.rootViewController as! ViewController
rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)
这几行代码可以正常工作,但是,如果用户在另一个视图控制器中(登录 in/login/user 页面等),这段代码将不起作用并重定向。 我尝试了一切,但仍然无法提出解决方案。我的最终目标是:
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 didReceiveRemoteNotification:(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;
}
}