【发布时间】:2015-04-05 11:42:04
【问题描述】:
我有一个应用程序,它应该在远程推送通知到来时加载一个 ViewController,我的 appDelegate.m 中有这个代码:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
NSLog(@"REMOTE NOTIFICATION %@", userInfo);
if( [[MTMAPIClient sharedClient] isLoggedIn: nil]){
if (userInfo && userInfo[@"aps"] && userInfo[@"aps"][@"extra"]) {
if ( application.applicationState == UIApplicationStateActive ) {
[self playSound];
}
}
[NotificationHelper pushNotificationCame:userInfo view:self.tabBarController.selectedViewController];
}
}
我的应用程序是一个选项卡式应用程序(如 AppStore 和 iTunes),我在 appDelegate 中保留了对 UITabBarController 的引用。
我的通知助手代码是:
+ (void) pushNotificationCame:(NSDictionary*)userInfo view:(UINavigationController*) viewController
{
NSString* otp;
if (userInfo && userInfo[@"aps"] && userInfo[@"aps"][@"extra"]) {
otp = (NSString*)userInfo[@"aps"][@"extra"][@"otp"];
[Authorization loadAuthByOtp:otp
success:^(Authorization *auth) {
dispatch_async(dispatch_get_main_queue(), ^{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"myStoryBoard" bundle:[NSBundle mainBundle]];
TransactionDetailViewController* appTransactionDetailsView = [storyboard instantiateViewControllerWithIdentifier:@"transactionDetails"];
appTransactionDetailsView.authorization = auth;
[viewController pushViewController:appTransactionDetailsView animated:YES];
});
} failure:^(NSString *error, ErrorType errorType) {
NSLog(@"pushNotificationCame loadAuthByOtp failed with error: %@", error);
}];
}
}
这适用于 70% 的场景,但有时当我退出应用程序并重新登录时,当推送通知出现时 self.tabBarController.selectedViewController 和 pushNotificationCame 方法中的“viewController”不是我选项卡中的当前活动视图栏所以 PushViewController 不起作用。
换句话说,当推送通知到来时,我的应用程序选项卡 A、B、C 中有三个选项卡选项卡 B 并没有任何反应。没有错误,但也没有新的 viewController。仅当我退出并重新登录应用程序时才会发生这种情况,通常它可以正常工作!
我的退出代码:
[viewController.navigationController.tabBarController performSegueWithIdentifier:@"logoutSegue" sender:viewController];
还有我的登录代码:
[self performSegueWithIdentifier:@"fromSignInToApplications" sender:self];
将用户带到 TabBarController 中的第一个选项卡。
在 AppDelegate 中
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.tabBarController = (UITabBarController *)self.window.rootViewController;
}
【问题讨论】:
-
注销/登录时你会做什么?
self.tabBarController正确吗? -
@Wain 我编辑了帖子以包含您要求的详细信息。
标签: ios objective-c iphone