I know this is gonna be an easy fix, but for some reason when i try to fix my issue with other solutions, it just doesnt work. In my app I set up Push Notifications, and I'm trying to set up the functionality of opening the app to a certain tabBarItem when the app is opened from a Notifications. here is the code i have so far, please tell me if I'm even using the right method. I'm not very experienced with AppDelegate.m so please forgive me if its completely wrong.
AppDelegate.m
#import <CoreLocation/CoreLocation.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate,CLLocationManagerDelegate>{
CLLocationManager *locationManager;
}
@property (strong, nonatomic) UIWindow *window;
@property(nonatomic,readonly) UITabBar *tabBar;
@property(nonatomic,strong) UITabBarController *tabBarController;
@end
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIApplication* app = [UIApplication sharedApplication];
app.networkActivityIndicatorVisible = YES;
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge |UIRemoteNotificationTypeSound)];
return YES;
}
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
/* this works just fine*/
NSLog(@"opening from a notification!!");
/*this is what doesnt work at all*/
self.tabBarController = [[UITabBarController alloc] init];
[self.tabBarController ];
self.tabBarController.selectedIndex = 3;
/*also when i do nslog the selectedIndex it gives me this value "2147483647"*/
NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}
Because you push to your tab bar controller from a login controller, there's no way to get a reference to the tab bar controller from the app delegate, because it doesn't exist until it's pushed. The way I would fix this is to change the structure of your app. While there's nothing wrong (as in it will cause a crash) with your current structure, I don't like putting login controllers first in line, because they're used once to log in, then it would be better if they would go away. I would make the tab bar controller the window's root view controller, and present the login controller modally, with no animation, from the viewDidAppear method of the controller in the first tab -- this will make the login controller the first thing the user sees. When you're done with login, you just dismiss, and your back to the first tab.
If you use this structure, then you can use my suggestion to set the selected index:
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
NSLog(@"opening from a notification!!");
self.tabBarController = self.window.rootViewController;
self.tabBarController.selectedIndex = 3;
NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}
For Local Notification:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// it from running if it doesn't exist (such as running under iOS 7 or earlier).
if ([application respondsToSelector:@selector(registerUserNotificationSettings:)]) {
[application registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound categories:nil]];
}
#endif
[UIApplication sharedApplication].applicationIconBadgeNumber = 0;
if ([UIApplication sharedApplication].scheduledLocalNotifications.count >= 1) {
// Handle local notification received if app wasn't running in background
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
}else{
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"tab"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
return YES;
}
For Push Notification:
(1).
- (void)application:(UIApplication*)application didReceiveRemoteNotification:(NSDictionary*)userInfo{
NSLog(@"opening from a push/remote notification!!");
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
NSLog(@"%i is the tabbar item",tabBarController.selectedIndex);
}
**(OR).** `
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
int badgeCount = [UIApplication sharedApplication].applicationIconBadgeNumber;
if (badgeCount >= 1) {
// Handle local notification received if app wasn't running in background
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
tabBarController.selectedIndex = 1;
}else{
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:@"tab"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
self.window.backgroundColor = [UIColor whiteColor];
return YES;
}
`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With