在AppDelegate中使用推送通知重定向视图

Rys*_*Rys 5 cocoa-touch push-notification ios uistoryboardsegue swift

我的目标是编写一个代码,当用户获得推送通知时,我希望将该用户重定向到另一个视图.如果用户带有推送通知,并且他是第一次查看控制器(欢迎主屏幕等(但未登录))

var rootViewController = self.window!.rootViewController as! ViewController
rootViewController.performSegueWithIdentifier("hospitalSegue", sender: self)
Run Code Online (Sandbox Code Playgroud)

这几行代码正在工作,但是,如果用户已经在另一个视图控制器(登录/登录/用户页面等),这段代码不起作用和重定向.我尝试了一切,但仍然无法提出解决方案.我的最终目标是:

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)
}
Run Code Online (Sandbox Code Playgroud)

谁能给我一个想法?

Mus*_*rci 1

答案代码是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
}
Run Code Online (Sandbox Code Playgroud)