从 didFinishLaunchingWithOptions 打开 ViewController
Open ViewController from didFinishLaunchingWithOptions
我正在向已终止的应用程序发送推送通知,似乎只触发了此方法。我想在使用推送通知启动应用程序时打开 ViewController,但它什么也没做,只是打开应用程序。
我试图用这段代码实现:
if (launchOptions != nil) {
NSDictionary* userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if (apsInfo)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"openNews" object:nil userInfo:userInfo];
}
}
也试过了..
if (launchOptions != nil) {
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"openNews" object:nil userInfo:userInfo];
}
}
关于如何启动 ViewController 当应用以推送通知终止时的任何想法?
所以我尝试将通知信息保存到 NSUserDefaults if launchOptions != nil 只是为了检查我是否收到通知信息并且那部分代码被触发了,但由于某种原因这部分不起作用:
[[NSNotificationCenter defaultCenter] postNotificationName:@"openNews" object:nil userInfo:userInfo];
但我在任何地方都使用相同的方法并且效果很好
这行不通,因为您发布的通知还没有人看到。
嗯,您在这里可以做的是在收到您指定的启动选项时设置应用程序的初始 ViewController。
你可以这样设置:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardId"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
在设计导航时要小心。特别是如果您希望初始 ViewController 成为带有访问导航堆栈的后退按钮的页面。
由于您要将其设置为初始视图控制器,因此如果它尝试弹出ViewController,它将弹出为空。
编辑:
如果你想让它从 MainVC 延迟打开,你可以把 tnis 放在你的 MainVC
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(openAnotherVC:)
name:@"YourPostNotificationName" object:nil];
然后在 openAnotherVC: 方法
中导航到您想要的 VC
我正在向已终止的应用程序发送推送通知,似乎只触发了此方法。我想在使用推送通知启动应用程序时打开 ViewController,但它什么也没做,只是打开应用程序。
我试图用这段代码实现:
if (launchOptions != nil) {
NSDictionary* userInfo = [launchOptions valueForKey:@"UIApplicationLaunchOptionsRemoteNotificationKey"];
NSDictionary *apsInfo = [userInfo objectForKey:@"aps"];
if (apsInfo)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"openNews" object:nil userInfo:userInfo];
}
}
也试过了..
if (launchOptions != nil) {
NSDictionary* userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (userInfo != nil)
{
[[NSNotificationCenter defaultCenter] postNotificationName:@"openNews" object:nil userInfo:userInfo];
}
}
关于如何启动 ViewController 当应用以推送通知终止时的任何想法?
所以我尝试将通知信息保存到 NSUserDefaults if launchOptions != nil 只是为了检查我是否收到通知信息并且那部分代码被触发了,但由于某种原因这部分不起作用:
[[NSNotificationCenter defaultCenter] postNotificationName:@"openNews" object:nil userInfo:userInfo];
但我在任何地方都使用相同的方法并且效果很好
这行不通,因为您发布的通知还没有人看到。
嗯,您在这里可以做的是在收到您指定的启动选项时设置应用程序的初始 ViewController。
你可以这样设置:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"YourStoryboard" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"YourStoryboardId"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
在设计导航时要小心。特别是如果您希望初始 ViewController 成为带有访问导航堆栈的后退按钮的页面。
由于您要将其设置为初始视图控制器,因此如果它尝试弹出ViewController,它将弹出为空。
编辑:
如果你想让它从 MainVC 延迟打开,你可以把 tnis 放在你的 MainVC
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(openAnotherVC:)
name:@"YourPostNotificationName" object:nil];
然后在 openAnotherVC: 方法
中导航到您想要的 VC