Get to specific tabView object from App Delegate - ios

Trying to launch a specific tabView object from the app delegate when you receive a notification, however the app crashes on launch.
Here is the code:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController;
CustomTabViewController * tabBar = [[CustomTabViewController alloc] init];
NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
NSString *type = [notificationPayload objectForKey:#"type"];
if([type isEqualToString:#"friend"]){
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:2];
self.window.rootViewController = tabBar;
[self.window makeKeyAndVisible];
}
if([type isEqualToString:#"message"]){
tabBar.selectedViewController = [tabBar.viewControllers objectAtIndex:0];
self.window.rootViewController = tabBar;
[self.window makeKeyAndVisible];
}
Any ideas?

Related

Hide login view if user logged in

i'm try to never show user login view if he logged in
i do this in viewWillAppear
-(void)viewWillAppear:(BOOL)animated
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"userName"]) {
NSDictionary *d = [defaults objectForKey:#"currentUser"];
UserProfile *userData = [[UserProfile alloc]initWithDictionary:d];
[[NetworkModel sharedManager] setCurrentUser:hh];
[self loginResults];
}
self.navigationController.navigationBarHidden = YES;
}
and loginResults function
-(void)childrenReceived:(NSNotification *) notification
{
[SVProgressHUD dismiss];
NSDictionary *userInfo = notification.userInfo;
NSArray *allKids = [userInfo objectForKey:#"children"];
RootViewController *sideBarRoot= [self.storyboard instantiateViewControllerWithIdentifier:#"root"];
sideBarRoot.children = allKids;
[self dismissViewControllerAnimated:YES completion:nil];
[self presentViewController:sideBarRoot animated:YES completion:nil];
}
check the below coding
#interface AppDelegate : UIResponder <UIApplicationDelegate>
-(void) didFinishLogin
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
CCKFNavDrawer*homeNavController = (CCKFNavDrawer*)[mainStoryboard instantiateViewControllerWithIdentifier:#"NavigationLoginID"];
[self.window makeKeyAndVisible];
self.window.rootViewController = homeNavController;
-(void)didFinishLogout
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *homeNavController = (UINavigationController*)[mainStoryboard instantiateViewControllerWithIdentifier:#"LoginNavigationID"];
LoginVC *objloginVC = (LoginVC*)[homeNavController topViewController];
objloginVC.delegate=self;
[self.window makeKeyAndVisible];
self.window.rootViewController = homeNavController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
if( [[[NSUserDefaults standardUserDefaults]objectforkey:#"loginKey"]isequaltostring:#""])
[self didFinishLogout];
else
[self didFinishLogin];

Creating a View Controller that only appears on first app open

I am creating a View Controller that only appears the first time the user opens the app. I had it working by just pushing the welcome view controller, but then I couldn't get it to push the normalViewController when the user was done in the welcomeViewController. I then then tried to set the welcomeViewController as the rootViewController. This still shows the welcomeScreen, but all of the buttons on it do not work. If any one knows how to fix this or a better way to create a welcomeViewController, it would be very much appreciated. Here is the code I am using to show the rootViewController
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"TermsAccepted"]){
NSLog(#"Second time opening the app");
}
else{
WelcomeViewController *welcomeViewController = [[WelcomeViewController alloc] init];
[[UIApplication sharedApplication] keyWindow].rootViewController = welcomeViewController;
}
To create the UITabBar -
self.tabBarController = [[UITabBarController alloc] init];
[[UITabBar appearance] setTintColor:[UIColor redColor]];
// FeedViewController
feedViewController=[[FeedViewController alloc] init];
feedViewController.tabBarItem.image=[UIImage imageNamed:#"Describe Home_Icon_NormalArtboard-1"];
feedViewController.title = #"Timeline";
feedViewController.tabBarItem.title = nil;
//TodayViewController
TodayViewController *todayViewController = [[TodayViewController alloc] init];
todayViewController.tabBarItem.image = [UIImage imageNamed:#"Today_Icon"];
todayViewController.title = #"Today";
todayViewController.tabBarItem.title = nil;
//CreateViewController
self.createViewController = [[CreateViewController alloc] init];
self.createViewController.tabBarItem.image = [UIImage imageNamed:#"Create_Icon"];
self.createViewController.title = #"Create";
self.createViewController.tabBarItem.title = nil;
//AlertViewController
AlertsViewController *alertsViewController = [[AlertsViewController alloc] init];
alertsViewController.tabBarItem.image=[UIImage imageNamed:#"Alerts_IconArtboard-1"];
alertsViewController.title=#"Alerts";
alertsViewController.tabBarItem.title = nil;
//ProfileViewController
ProfileViewController *profileViewController = [[ProfileViewController alloc] init];
profileViewController.tabBarItem.image=[UIImage imageNamed:#"Profile_IconArtboard-1"];
profileViewController.title=#"Profile";
profileViewController.tabBarItem.title = nil;
NSMutableArray *tabBarViewControllers = [[NSMutableArray alloc] initWithCapacity:2];
self.tabBarController = [[UITabBarController alloc] init];
UINavigationController *feedNavigationController = [[UINavigationController alloc] initWithRootViewController:feedViewController];
[tabBarViewControllers addObject:feedNavigationController];
feedNavigationController = nil;
UINavigationController *todayNavigationController = [[UINavigationController alloc] initWithRootViewController:todayViewController];
[tabBarViewControllers addObject:todayNavigationController];
todayNavigationController = nil;
UINavigationController *createNavigationController = [[UINavigationController alloc] initWithRootViewController:self.createViewController];
[tabBarViewControllers addObject:createNavigationController];
createNavigationController = nil;
UINavigationController *alertsNavigationController = [[UINavigationController alloc] initWithRootViewController:alertsViewController];
[tabBarViewControllers addObject:alertsNavigationController];
alertsNavigationController = nil;
UINavigationController *profileNavigationController = [[UINavigationController alloc] initWithRootViewController:profileViewController];
[tabBarViewControllers addObject:profileNavigationController];
profileNavigationController = nil;
self.tabBarController.viewControllers = tabBarViewControllers;
tabBarViewControllers = nil;
[self.window addSubview:self.tabBarController.view];
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"TermsAccepted"]){
NSLog(#"Second time opening the app");
rootViewController = // Your new main controller
}
else
{
rootViewController = // Terms and conditions view controller
}
self.window.rootViewController = rootViewController;
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
self.window.rootViewController = navigationController;
return YES;
}

IOS: Load a View Controller When App is Launched from Browser using url schemes?

I am new to iPhone developing , My app is based on video conferencing, I have a task where an app should launch from browser using URL schemes. I have done it but problem is when app launched from browser it should load particular view controller. I am using Storyboard. Here is the code which I tried.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
if (!url) { return NO;
}
NSString *URLString = [url absoluteString];
[[NSUserDefaults standardUserDefaults] setObject:URLString forKey:#"url"];
[[NSUserDefaults standardUserDefaults] synchronize];
URLParser *parser = [[URLParser alloc] initWithURLString:URLString];
username = [parser valueForVariable:#"USERNAME"];
NSLog(#"%#", username); //b
sessid = [parser valueForVariable:#"SESSION_ID"];
NSLog(#"%#", sessid); //(null)
tokenid = [parser valueForVariable:#"token"];
NSLog(#"%#", tokenid); //yes
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
return YES;
}
This is how I do it..
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
//get parameters
[self goResetPassword:dict];
}
return YES;
}
- (void) goResetPassword:(NSDictionary*) dict{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
UINavigationController *root = [[UINavigationController alloc]initWithRootViewController:[storyboard instantiateViewControllerWithIdentifier:#"resetPasswordView"]];
self.window.rootViewController= root;
ResetPasswordViewController *vc = (ResetPasswordViewController*)[[root viewControllers] objectAtIndex:0];
[vc loadData:dict];
}
hope it helps... GL HF
According to your comment your initial ViewController is NavigationController. This is how you can push the required ViewController.
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url
{
// same code
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
// Get instance of initial Viewcontroller from storyboard
UINavigationController *navController = [storyboard instantiateInitialViewController
];
// Get instance of desired viewcontroller
ViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"VideoController"];
// Push ViewController on to NavigationController
[navController pushViewController:viewController animated:NO];
return YES;
}
Check It
UIStoryboard *board = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
UINavigationController *nav = [[UINavigationController alloc] init];
[nav setNavigationBarHidden:NO];
IntroVC *initailView = [board instantiateViewControllerWithIdentifier:#"Intro"];
[nav pushViewController:initailView animated:YES];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
UIApplication *sharedApplication = [UIApplication sharedApplication];
[sharedApplication unregisterForRemoteNotifications];

Tab Bar Not Showing

I have a specific view opening when opening my app from a notification. The navigation bar shows up and the navigation works correctly. The problem I'm having is getting the tab bar to show up. Here is my didFinishLaunchingWithOptions method.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// This prevents the UA Library from registering with UIApplcation by default when
// registerForRemoteNotifications is called. This will allow you to prompt your
// users at a later time. This gives your app the opportunity to explain the benefits
// of push or allows users to turn it on explicitly in a settings screen.
// If you just want everyone to immediately be prompted for push, you can
// leave this line out.
// [UAPush setDefaultPushEnabledValue:NO];
//Create Airship options dictionary and add the required UIApplication launchOptions
NSMutableDictionary *takeOffOptions = [NSMutableDictionary dictionary];
[takeOffOptions setValue:launchOptions forKey:UAirshipTakeOffOptionsLaunchOptionsKey];
// Call takeOff (which creates the UAirship singleton), passing in the launch options so the
// library can properly record when the app is launched from a push notification. This call is
// required.
//
// Populate AirshipConfig.plist with your app's info from https://go.urbanairship.com
[UAirship takeOff:takeOffOptions];
// Set the icon badge to zero on startup (optional)
[[UAPush shared] resetBadge];
// Register for remote notfications with the UA Library. This call is required.
[[UAPush shared] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeBadge |
UIRemoteNotificationTypeSound |
UIRemoteNotificationTypeAlert)];
// Handle any incoming incoming push notifications.
// This will invoke `handleBackgroundNotification` on your UAPushNotificationDelegate.
[[UAPush shared] handleNotification:[launchOptions valueForKey:UIApplicationLaunchOptionsRemoteNotificationKey]
applicationState:application.applicationState];
// self.tabBarController = [[UITabBarController alloc] initWithNibName:#"KFBViewController" bundle:nil];
KFBViewController *rootView = [[KFBViewController alloc] initWithNibName:#"KFBViewController" bundle:nil];
KFBNavControllerViewController *navController = [[KFBNavControllerViewController alloc] initWithRootViewController:rootView];
navController.delegate = rootView;
UIViewController *aboutUs = [[AboutUs alloc] initWithNibName:#"AboutUs" bundle:nil];
KFBNavControllerViewController *navController1 = [[KFBNavControllerViewController alloc] initWithRootViewController:aboutUs];
UIViewController *contactUs = [[ContactUs alloc] initWithNibName:#"ContactUs" bundle:nil];
KFBNavControllerViewController *navController2 = [[KFBNavControllerViewController alloc] initWithRootViewController:contactUs];
UIViewController *kyfb = [[KYFB alloc] initWithNibName:#"KYFB" bundle:nil];
KFBNavControllerViewController *navController3 = [[KFBNavControllerViewController alloc] initWithRootViewController:kyfb];
// UIViewController *rsfm = [[RSFM alloc] initWithNibName:#"RSFM" bundle:nil];
// KFBNavControllerViewController *navController4 = [[KFBNavControllerViewController alloc] initWithRootViewController:rsfm];
// UIViewController *li = [[LegislatorInfo alloc] initWithNibName:#"LegislatorInfo" bundle:nil];
// KFBNavControllerViewController *navController5 = [[KFBNavControllerViewController alloc] initWithRootViewController:li];
// UIViewController *events = [[Events alloc] initWithNibName:#"Events" bundle:nil];
// KFBNavControllerViewController *navController6 = [[KFBNavControllerViewController alloc] initWithRootViewController:events];
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//self.viewController = [[KFBViewController alloc] initWithNibName:#"KFBViewController" bundle:nil];
//self.window.rootViewController = self.viewController;
self.tabBarController = [[KFBTabBarViewController alloc] init];
self.tabBarController.viewControllers = #[navController, navController1, navController2, navController3];
// self.tabBarController.customizableViewControllers = nil;
self.window.rootViewController = self.tabBarController;
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
// If application is launched due to notification,present another view controller.
UILocalNotification *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification)
{
ActionAlertsViewController *actionAlerts = [[ActionAlertsViewController alloc] initWithStyle:UITableViewStylePlain];
WebViewController *wvc = [[WebViewController alloc]init];
[actionAlerts setWebViewController:wvc];
KFBNavControllerViewController *navController7 = [[KFBNavControllerViewController alloc] initWithRootViewController:actionAlerts];
[self.window.rootViewController presentViewController:navController7 animated:NO completion:nil];
}
return YES;
}

Accessing a UITableView when a UINavigationController gets in the way (in a UISplitViewController)

I have a UISplitViewController that is set up like so:
-(IBAction)makeStory:(id)sender{
NSLog(#"makeStory:");
makeStoryTableViewController = [[MakeStoryTableViewController alloc] initWithNibName:#"MakeStoryTableViewController" bundle:nil];
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:#"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
splitViewController.delegate = makeStoryTableViewController;
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window setRootViewController:splitViewController];
}
When I try to set detailView in the UISplitView delegate as a result of didSelectRowAtIndexPath:, I can only access the NavigationController:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSLog(#"ViewControllers: %#", [self.splitViewController viewControllers]);
MakeSentenceTableViewController *detailViewController = [[self.splitViewController viewControllers] objectAtIndex:1];
Story *storySet = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(#"detailViewController: %#", detailViewController); //Logs a UINavigationController
[detailViewController setStory:storySet]; //Fails here because Navigation Controllers Can't setStory!
[detailViewController refreshTables];
}
Which makes sense, but how do I access the TableViews that the UINavigationControllers are responsible for? If I remove *rootNav and *detailNav and replace them with the UITableViews in the splitViewController.viewControllers statement it works fine, but then I have no navigation bar:
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:#"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
//UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
//UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:makeStoryTableViewController, detailViewController, nil];
splitViewController.delegate = makeStoryTableViewController;
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.window setRootViewController:splitViewController];
How can I access the Detail TableView Controller and keep the Navigation Bar?
Here's how I figured it out. I made two mistakes, and in those mistakes I had to use the UINavigationController.viewControllers ObjectAtIndex: property.
In the function makeStory I was incorrectly assigning a UINavigationController as a delegate instead of a makeStoryTableViewController. I fixed it by doing this, instead (pay special attention to the splitViewController.delegate section):
MakeSentenceTableViewController *detailViewController = [[MakeSentenceTableViewController alloc] initWithNibName:#"MakeSentenceTableViewController" bundle:nil];
UISplitViewController *splitViewController = [[[UISplitViewController alloc] init] autorelease];
UINavigationController *rootNav = [[[UINavigationController alloc] initWithRootViewController:makeStoryTableViewController]autorelease];
UINavigationController *detailNav = [[[UINavigationController alloc] initWithRootViewController:detailViewController] autorelease];
splitViewController.viewControllers = [NSArray arrayWithObjects:rootNav, detailNav, nil];
//splitViewController.viewControllers = [NSArray arrayWithObjects:makeStoryTableViewController, detailViewController, nil];
//RootViewController *root = (RootViewController *)[navigationController.viewControllers objectAtIndex:0]
splitViewController.delegate = [rootNav.viewControllers objectAtIndex:0];
NSLog(#"delegate: %#", [rootNav.viewControllers objectAtIndex:0]);
StoryBotAppDelegate *appDelegate = (StoryBotAppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(#"RootViewController: %#", appDelegate.window);
[appDelegate.window setRootViewController:splitViewController];
NSLog(#"AppDelegate.window: %#", appDelegate.window.rootViewController);
I made the same mistake again, this time in the didSelectRowAtIndexPath: and tried to setStory in a UINavigationController. To fix it, I did this (pay special attention to the bit about navControllerDetail.viewControllers:
UINavigationController *navControllerDetail;
navControllerDetail = [[self.splitViewController viewControllers] objectAtIndex:1];
MakeSentenceTableViewController *detailViewController;
detailViewController = [navControllerDetail.viewControllers objectAtIndex:0];
Story *storySet = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(#"detailViewController: %#", detailViewController);
[detailViewController setStory:storySet];
[detailViewController refreshTables];
Now if only I could figure out how to get the SplitViewController to rotate properly!

Resources