iOS通过本地通知显示特定的viewcontroller
我已经在我的应用程序中实现了本地通知,但我想选择在用户“刷”通知时显示哪个viewcontroller。我的应用程序是几个视图控制器,它们之间有基本的分段导航iOS通过本地通知显示特定的viewcontroller,ios,objective-c,uiviewcontroller,uilocalnotification,Ios,Objective C,Uiviewcontroller,Uilocalnotification,我已经在我的应用程序中实现了本地通知,但我想选择在用户“刷”通知时显示哪个viewcontroller。我的应用程序是几个视图控制器,它们之间有基本的分段导航 如何选择要查看的viewcontroller?您可以在application:didFinishLaunchingWithOptions方法中处理本地通知,然后您可以选择右视图控制器: - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOption
如何选择要查看的viewcontroller?您可以在application:didFinishLaunchingWithOptions方法中处理本地通知,然后您可以选择右视图控制器:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Handle notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
UIViewController *vc = nil
if (localNotif) {
//base on notification create right view controller
vc = [[VC alloc] init];
NSLog(@"Recieved Notification %@",localNotif);
}
else
{
//create default view controller
vc = [[VC alloc] init];
}
// Add the view controller's view to the window and display.
_window.rootViewController = vc;
[_window makeKeyAndVisible];
return YES;
}
Greg给出了大部分答案,但请注意,除了didFinishLaunchingWithOptions之外,还需要添加一个application:didReceiveLocalNotification:method。一旦应用程序运行,就会调用该方法。我建议将处理本地通知的代码提取到一个方法中,并从两个位置调用它
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
[[UIApplication sharedApplication] cancelLocalNotification:notification];
//My_specificViewController
RingingViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"RingingViewController"];
[self.window setRootViewController:ringingVC];
}