iOS Push a specific view when notification arrives - iphone

- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#",notif);
NSLog(#"local notifications count = %d", [[[UIApplication sharedApplication] scheduledLocalNotifications] count]);
CFBundleRef mainBundle = CFBundleGetMainBundle();
CFURLRef soundFileURLRef;
soundFileURLRef =CFBundleCopyResourceURL(mainBundle, (CFStringRef) #"everything9", CFSTR ("mp3"), NULL);
UInt32 soundID;
AudioServicesCreateSystemSoundID(soundFileURLRef, &soundID);
AudioServicesPlaySystemSound(soundID);
[[NSNotificationCenter defaultCenter] postNotificationName:#"RELOAD_DATA"object:nil];
}
What can I implement here to push a specific view when the application arrives (when the user slides the app icon when the iPhone is locked, for example)... I'm trying [self.navigationController pushViewController:CompletedViewController animated:YES]; and I get some errors... Is there a specific way I should do it? Maybe in didFinishLaunchingWithOptions?

maybe you should call this
[_window.rootViewController pushViewController:CompletedViewController animated:YES];
The code above doesn't work now. You should try using this instead.
//We take the rootViewController first which is expected to be a UINavigationController in your case
UINavigationController *naviController = _window.rootViewController;
//We then call the push view controller code
[naviController pushViewController:CompletedViewController animated:YES];
this is how you push to your current navigation controller from the AppDelegate if you are using storyboards. Specially if your starting point on the story board is a navigation controller.

I think this will fit your needs. It will teach how to use a UINavigationController inside the AppDelegate. If you have further questions let me know.

In the older versions of Xcode, We have an option while creating the applications is navigation based applications. But in the latest Xcode generations, we can create Single view based or window based applications. So, if you want to use the navigation controller properties, you need to create the instance for UINavigationController and need to set the rootViewController. The code is as follows,
appDelegate.h
#property (strong, nonatomic) UINavigationController *navigationController;
appDelegate.m
#implementation AppDelegate
#synthesize navigationController = _navigationController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[ViewController alloc] initWithNibName:#"ViewController" bundle:nil] autorelease];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
So that, you can use navigation properties throughout the application.
I think this may helpful to you.

"[self.navigationController pushViewController:viewController animated:YES];”
This can’t be used from AppDelegate.m (ie. from didReceiveLocalNotification).
Try once by using “self.pushViewController” from didReceiveLocalNotification.

if ([EntNotStrApp isEqualToString:#"1"])
{
EntNotStrApp=#"0";
UIWindow *window = [[[UIApplication sharedApplication] windows] objectAtIndex:0];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
UINavigationController *navController = (UINavigationController *)window.rootViewController;
DumpFeed *dump = [storyboard instantiateViewControllerWithIdentifier:#"DumpFeed"];
dump.isPushed=YES;
dump.strUserId = appDelegate.strFriendid;
[navController pushViewController:dump animated:YES];
}else
{
[[iToast makeText:NotMess] show];
}

Related

Switching to tabBarController but no tab bar

I apologize for asking two similar questions but I don't think I worded it correctly the first time and am still struggling to find the answer.
I am in a viewcontroller that is in a project containing a tabBarController. I want to switch from the viewController to one of the viewcontrollers contained in the tabbarcontroller. The problem is it either doesn't show up at all or if I present the normal viewcontroller there is no tab bar.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UIViewController *viewController1 = [[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil] autorelease];
self.tabBarController.viewControllers = #[viewController1, viewController2,viewController3);
}
I want to switch from my Tag view controller to FirstViewController
Tag.m
-(IBAction)save:(id)sender{
FirstViewController*vc =[[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
[self.tabBarController.viewControllers popToViewController:vc animated:YES];
}
Update:
I solved this by adding my ViewController to a TabBar within my .m file then
[_tabBarController setSelectedIndex:0];
[self presentViewController: _tabBarController animated:YES completion:NULL];
Thanks for the responses I got on this question.
You can change rootViewController property of mainWindow of AppDelegate some thing like this.
AppDelegate * appDel = (AppDelegate*)[[UIApplication sharedApplication] delegate];
[appDel.window setRootViewController:tabBarController];
Enjoy..

App not navigating to second view

The code I am using
NSLog(#"before Navigate to second activity");
Signature *temp = [[Signature alloc]initWithNibName:#"Signature" bundle:nil];
[self.navigationController pushViewController:temp animated:YES];
NSLog(#"after Navigate to second activity");
On console,both the log statements are getting printed, but my app is not navigating me to the next View.Please correct me.
if you are not using a UINavigationController on your app you can not call pushViewController, use presentViewController:animated:completion: instead:
[self presentViewController:temp animated:YES completion:nil];
for more information check the documentation
The log messages would show anyway, so no surprise there. Add a callback to the delegate for that navigation controller (you must first set a delegate of course) for
navigationController:didShowViewController:animated:
There you can make sure that the viewController passed (make sure Signature is a ViewController instance).
i think your application is not with navigationController so in AppDelegate.m file assign rootViewController like this..
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
RootViewController *viewController1 = [[[RootViewController alloc] initWithNibName:#"RootViewController" bundle:nil] autorelease];
UINavigationController *navviewController=[[UINavigationController alloc]initWithRootViewController:viewController1];
self.window.rootViewController = navviewController;
[self.window makeKeyAndVisible];
return YES;
}
after that check its working..
CreditsViewController *viewController = [[CreditsViewController alloc] init];
[[self navigationController] pushViewController:viewController animated:YES];
[viewController release];
you can try with above code

How to launch view controller from alarm notification action in iOS?

In my app i am using alarm functionality. It working fine. When i click the right button it launches my app. But i want to launch View Controller which is not an rootViewController. I am tried in searching in Google and SO but i couldn't get any idea or example.
I am looking for any example to achieve this.?
Thanks for your help guys.
EDIT
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Add the view controller's view to the window and display.
[self.window addSubview:alarmViewController.view];
[self.window makeKeyAndVisible];
application.applicationIconBadgeNumber = 0;
// Handle launching from a notification
UILocalNotification *localNotif =
[launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if (localNotif) {
NSLog(#"Recieved Notification %#",localNotif);
window = [[UIApplication sharedApplication] keyWindow];
UIViewController *rootViewController = [window rootViewController];
[rootViewController presentModalViewController:receipeViewController animated:YES];
}
// Override point for customization after application launch.
return YES;
}
- (void)application:(UIApplication *)app didReceiveLocalNotification:(UILocalNotification *)notif {
// Handle the notificaton when the app is running
NSLog(#"Recieved Notification %#",notif);
//Called here as well
window = [[UIApplication sharedApplication] keyWindow];
UIViewController *rootViewController = [window rootViewController];
[rootViewController presentModalViewController:receipeViewController animated:YES];
}
Finally Here's how I did it.
In didFinishLaunchingWithOptions:
//save the root view controller
[[self window] makeKeyAndVisible];
UINavigationController *navigationController = (UINavigationController*) self.window.rootViewController;
rootController = [[navigationController viewControllers] objectAtIndex:0];
Somewhere else in the app delegate:
[rootController performSegueWithIdentifier:#"destinationSegue" sender:self];
Then, in the storyboard, create a segue from the view that gets assigned to "rootController" to the desired optional view, and give that new segue the id destinationSegue. It takes some debugging to make sure the rootController variable gets assigned to the correct view.
You do not need to create a segue.You only need to assign an id in the storyboard for the ViewController.
[[self window] makeKeyAndVisible];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
LensOverviewViewController *editLensViewController = [storyboard instantiateViewControllerWithIdentifier:#"lensOverView"];
UINavigationController *yourViewController = [[UINavigationController alloc] initWithRootViewController:editLensViewController];
[self.window.rootViewController presentViewController:yourViewController animated:NO completion:nil];
Normally [[self window] makeKeyAndVisible]; does not reside in the didFinishLaunchingWithOptions function but it needs to be called explicity to make the rootviewcontroller visible.
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIViewcontroller *rootViewController = [window rootViewController];
[rootViewController presentModalViewController:alarmViewController animated:YES];
When your application is launched, look for the UIApplicationLaunchOptionsLocalNotificationKey on the launchOptions dictionary on the following method:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
This will let you know if your application is being launched because of a local notification (the one you use for the alarm).
You can also do something similar on:
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
Once you determine that you actually received a notification, just find the root view controller from the window and present the view controller you want to present modally.

iphone app - uitabbarcontroller to always select first view in a uinavigationcontroller?

I am building an iphone application.
I have a tabbarcontroller that has 2 tab items. Each tabitem links to a different navigationcontroller. Each navigationcontroller links to a hierarchy of tableviewcontrollers.
When a user clicks on tab 1, then clicks on an item in the table, then clicks tab 2, and then clicks on tab1, the application shows the table that he was just looking at before he clicked on tab2.
How do i get the app to show the first table of tab 1 every time he clicks on tab 1 instead of showing the most recent table he was looking at before leaving tab1?
I would prefer a programmatic solution as opposed to using xcode storyboard. But if none exists, then storyboard solution is fine too.
Call popToRootViewControllerAnimated: on the NavigationController when the TabBarController changes tab that is being displayed.
Try this basic sample to create a UItabBar and UInavigationController for each UItabBarItem from scratch :
in your header file (appdelegate.h) , add this delegate :
#interface AppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
in the function called "didFinishLaunchingWithOptions" , add this part of code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UINavigationController *navController=[[UINavigationController alloc] init];
m_ViewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
[navController pushViewController:m_ViewController1 animated:NO];
UINavigationController *navController2=[[UINavigationController alloc] init];
m_ViewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
[navController pushViewController:m_ViewController2 animated:NO];
UITabBarController *mtabBarController = [[UITabBarController alloc] init];
mtabBarController.view.frame = CGRectMake(0, 0, 320, 460);
// Set each tab to show an appropriate view controller
[mtabBarController setViewControllers: [NSArray arrayWithObjects:navController1,navController1,navController2, nil]];
self.window.rootViewController = mtabBarController;
mtabBarController.delegate = self;
[self.window makeKeyAndVisible];
return YES;
}
Then in this function , don't forget to add the popToRootViewControllerAnimated function :
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
[m_ViewController1.navigationController popToRootViewControllerAnimated:YES];
[m_ViewController2.navigationController popToRootViewControllerAnimated:YES];
return YES;
}
in my appdelegate.h file, I changed the line
#interface wscAppDelegate : UIResponder <UIApplicationDelegate>
to
#interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
Then in my CustomTabBarController in the viewDidLoad function i added these lines:
wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;
Then in appdelegate.m file, I added this function
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
for(int c=0; c<[tabBarController.viewControllers count]; c++)
{
UINavigationController * navcontroller = [tabBarController.viewControllers objectAtIndex:c];
[navcontroller popToRootViewControllerAnimated:YES];
}
return YES;
}

Add AdWhirl view to a tabBarController

I am trying to add an Adwhirl view to the top of my current iOS application. The application is composed of five different views, which are all under the control of a single TabBarController. can anyone write a brief tutorial that shows the code required to achieve this? I have looked through and tried a lot of the solutions out there but none of them are making it work.
Below is my current attempt at the problem, I don't get errors but I don't see anything different on the screen. Thanks in advance.
#implementation idoubs2AppDelegate
#synthesize window;
#synthesize tabBarController;
#synthesize contactsViewController;
#synthesize messagesViewController;
#synthesize adwhirlview = adview;
static UIBackgroundTaskIdentifier sBackgroundTask = UIBackgroundTaskInvalid;
static dispatch_block_t sExpirationHandler = nil;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
AdWhirlView *adView = [AdWhirlView requestAdWhirlViewWithDelegate:self];
[self.tabBarController.view addSubview:adView];
adView.center = CGPointMake(160, 342);
[self.tabBarController.view bringSubviewToFront:adView];
}
- (NSString *)adWhirlApplicationKey {
// return your SDK key
return kSampleAppKey;
}
- (UIViewController *)viewControllerForPresentingModalView {
//return UIWindow.viewController;
return [(idoubs2AppDelegate *)[[UIApplication sharedApplication] delegate] tabBarController];
}
- (void)adWhirlDidReceiveAd:(AdWhirlView *)adWhirlView {
}
You probably shouldn't be adding subviews to a normal UITabBar, if you need to customize the layout of your tab bar you might want to take a look at some of the replacements here: http://cocoacontrols.com/
I think instead of adding subviews to a UITabBar, you may think of making something similar to a GADBannerView singleton that you could just then add to each o the ViewControllers that your UITabBarController contains.
So you can set up a singleton easily enough (there's another StackOverflow question here that talks about how to do this for AdMob ads and making it work with AdWhirl should be trivial).
Then just add some ViewControllers into your UITabBarController doing something like:
UIViewController *viewController1 = [[[FirstViewController alloc]
initWithNibName:#"FirstViewController"
bundle:nil]
autorelease];
UIViewController *viewController2 = [[[SecondViewController alloc]
initWithNibName:#"SecondViewController"
bundle:nil]
autorelease];
self.tabBarController = [[[UITabBarController alloc] init] autorelease];
self.tabBarController.viewControllers = [NSArray arrayWithObjects:
viewController1,
viewController2,
nil];