How to open an existing viewcontroller in ios - ios

I need to open existing viewcontroller from AppDelegate while receiving push notification. Currently i am opening new one every time so issue is that it is called viewDidLoad every time and all variable are reinitialized again and again.
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSUserDefaults standardUserDefaults] setObject:#"Yes" forKey:#"Got Message"];
[[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:#"message"];
[[NSUserDefaults standardUserDefaults]synchronize];
HomeViewController* room = [[HomeViewController alloc] init];
[self.window.rootViewController presentViewController:room
animated:NO
completion:nil];
}

Try to do so:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
[[NSUserDefaults standardUserDefaults] setObject:#"Yes" forKey:#"Got Message"];
[[NSUserDefaults standardUserDefaults] setObject:userInfo forKey:#"message"];
[[NSUserDefaults standardUserDefaults]synchronize];
[self.window.rootViewController presentViewController:self.room
animated:NO
completion:nil];
}
- (UIViewController *)room {
if (_room == NULL) {
_room = [[HomeViewController alloc] init];
}
return _room;
}
Then you can reuse your view controller (However, it will exposure the view controller in your AppDelegate, which may be a taste in clean code).

Since you want to use a existing view controller, why do you use the code HomeViewController* room = [[HomeViewController alloc] init];?
Follow your goal, my suggestion is use a property to retain the existing view controller, just like:
#property (strong, nonatomic) UIViewController *existingViewController;
and you
[self.window.rootViewController presentViewController:existingViewController
animated:NO
completion:nil];

You can get UINavigationController in AppDelegate meethod
UIViewController *yourViewController = //your view controller to show after push
UINavigationController *navController = self.window.rootViewController;
[navController popToViewController:yourViewController animated:YES]

Related

How to call a function that is found in a specific view controller at the moment the application is launched?

I have some functions in my SecondViewController.m file and I want to call these functions when the application is launched. I have tried to do it like this, but it’s not working.
I have put a print statement in the function to see if it is getting called or not and it appears that the print statement is executed correctly.
Here is the following code:
In SecondViewController.h:
#interface AlertsManagementController : UIViewController {
PushNotificationSettings *pushNotificationSettings;
IBOutlet UISwitch *switch1;
IBOutlet UISwitch *switch2;
}
#property (nonatomic, retain) PushNotificationSettings *pushNotificationSettings;
In SecondViewController.m:
- (void)viewDidLoad {
[super viewDidLoad];
appDelegate = (projectAppDelegate *)[[UIApplication sharedApplication] delegate];
pushNotificationSettings = [[PushNotificationSettings alloc] init];
NSDictionary * settings = [pushNotificationSettings getPushSettings];
}
-(void)modificationPush
{
if (switch1.on && switch2.on)
[switch2 setOn:NO];
printf("Function 1 executed!");
}
-(void)sendTokenFunc{
NSMutableDictionary *preferences1 = [[NSMutableDictionary alloc] init];
if (switch1.on)
[preferences1 setObject:#"1" forKey:#“switch1”];
else
[preferences1 setObject:#"0" forKey:#“switch1”];
if (switch2.on)
[preferences1 setObject:#"1" forKey:#“switch2”];
else
[preferences1 setObject:#"0" forKey:#“switch2”];
[pushNotificationSettings savePushSettingsWithDictionary:preferences1];
[preferences1 release];
[appDelegate uploadToken:[appDelegate tokenValue]];
printf("Function 2 executed!");
}
In appDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
SecondViewController * vc = [[SecondViewController alloc]init];
[vc modificationPuch];
[vc sendTokenFunc];
}
This can be done by using NSNotificationCenter.
First go to your SecondViewController.m file and in viewDidLoad write the following code.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(myNotification:) name:#"myNotification" object:nil];
Then outside viewDidLoad make a function myNotification. I will show you the eg below
-(void)myNotification:(NSNotification*)notification
{
//write the code you wanna excecute when the app opens here
}
Go to appDeligate.m and write this code in application didFinishLaunchingWithOptions
[[NSNotificationCenter defaultCenter] postNotificationName:#"myNotification" object:self userInfo:nil];
Thank you,
Happy coding;)
But if SecondViewController didn't inited, you must get EXC_BAD_ACCESS :)

Unable to mock NSUserDefaults

I don't seem to be able to mock NSUserDefaults. I want it to send back the data that I tell it too but instead, it sends back the data stored in it from when running the app.
I am using:
Specta
Expecta
OCMockito
I have the following test:
describe(#"AppDelegate", ^{
__block AppDelegate *appDelegate;
beforeEach(^{
appDelegate = [AppDelegate new];
});
afterEach(^{
appDelegate = nil;
});
describe(#"application did finish launching with options", ^{
beforeEach(^{
[appDelegate application:nil didFinishLaunchingWithOptions:nil];
NSUserDefaults *mockUserDefaults = mock([NSUserDefaults class]);
[given([mockUserDefaults objectForKey:#"currentUser"]) willReturn:nil];
});
it(#"should have a login view controller as root view controller", ^{
expect(appDelegate.window.rootViewController).to.beKindOf([ToALoginViewController class]);
});
});
});
So the above test fails because it actually returns some data for currentUser. What am I doing wrong?
Following on from what Ken Kuan said, I made the following changes:
AppDelegate.h
#property (strong, nonatomic) NSUserDefaults *userDefaults;
- (void)setUserDefaults:(NSUserDefaults *)userDefaults;
AppDelegate.m
- (void)setUserDefaults:(NSUserDefaults *)userDefaults {
_userDefaults = userDefaults;
}
AppDelegateSpec.m
beforeEach(^{
[appDelegate application:nil didFinishLaunchingWithOptions:nil];
NSUserDefaults *mockUserDefaults = mock([NSUserDefaults class]);
[appDelegate setUserDefaults:mockUserDefaults];
[given([mockUserDefaults objectForKey:#"currentUser"]) willReturn:nil];
});
However, I still get the same problem.
You can mock NSUserDefaults. I have this code which is working fine.
When you are mocking NSUserdefaults make sure you are stopping mock at the end of test case, otherwise it would cause errors in other places where you are using NSUserDefaults.
id userDefaultsMock = OCMClassMock([NSUserDefaults class]);
OCMStub([userDefaultsMock standardUserDefaults]).andReturn(userDefaultsMock);
OCMStub([[userDefaultsMock standardUserDefaults] boolForKey:SHOWMYNAME]).andReturn(#"N");
[self.myObject codewithuserdefaults];
OCMVerify( [[NSUserDefaults standardUserDefaults] setValue:[OCMArg any] forKey:FIRSTNAME]);
[userDefaultsMock stopMocking];
You must use [NSUserDefaults standardUserDefaults] in appDelegate which is actual user defaults, not your mock.
A solution is to make user default to a property of app delegate and set it with your mockUserDefaults in tests.
Another one is to swizzle [NSUserDefaults standardUserDefaults] to return your mockUserDefaults in your tests.
So with much thanks to Ken Kuan, I managed to solve the problem I was having. Heres how I achieved it.
I added a NSUserDefaults property to the AppDelegate and a setter function.
I set self.userDefaults to [NSUserDefaults standardUserDefaults] in application:willFinishLaunchingWithOptions:.
I then set the rootViewController in application:didFinishLaunchingWithOptions:
AppDelegate.h
#property (strong, nonatomic) NSUserDefaults *userDefaults;
- (void)setUserDefaults:(NSUserDefaults *)userDefaults;
AppDelegate.m
- (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.userDefaults = [NSUserDefaults standardUserDefaults];
return YES;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
NSData *encodedUserData = [self.userDefaults objectForKey:#"currentUser"];
if (encodedUserData) {
NSLog(#"Have current user");
self.window.rootViewController = [ThermostatViewController new];
} else {
NSLog(#"No current user");
self.window.rootViewController = [ToALoginViewController new];
}
[self.window makeKeyAndVisible];
return YES;
}
- (void)setUserDefaults:(NSUserDefaults *)userDefaults {
_userDefaults = userDefaults;
}
AppDelegateSpec.m
describe(#"application will finish launching with options", ^{
beforeEach(^{
[appDelegate application:nil willFinishLaunchingWithOptions:nil];
});
context(#"application did finish launching with options and with no current user", ^{
beforeEach(^{
NSUserDefaults *mockUserDefaults = mock([NSUserDefaults class]);
[appDelegate setUserDefaults:mockUserDefaults];
[given([mockUserDefaults objectForKey:#"currentUser"]) willReturn:nil];
[appDelegate application:nil didFinishLaunchingWithOptions:nil];
});
it(#"should have a login view controller as root view controller", ^{
expect(appDelegate.window.rootViewController).to.beKindOf([ToALoginViewController class]);
});
});
});

Delete all user data in UIViewController's after logout

When I login to my app, my app does push the ViewController XYZMainViewController, XYZMainViewController viewWillAppear:animated call method that makes a request to my API to retrieve the authenticated user data, at this time I update the text of a label to show the user name. When I logout the app, it returns me to the login ViewController, when I do login again with another user, XYZMainViewController label text contains the name of the previous user, without updating the label text.
XYZMainViewController.m
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self homeProfile];
}
- (void)homeProfile
{
[NXOAuth2Request performMethod:#"GET"
onResource:[NSURL URLWithString:#"http://{url}/users/userinfo"]
usingParameters:nil
withAccount:[XYZCommonFunctions user]
sendProgressHandler:nil
responseHandler:^(NSURLResponse *response, NSData *responseData, NSError *error){
NSDictionary *parsedData = [[NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error] objectForKey:#"data"];
_user = [parsedData objectForKey:#"user"];
[self.label setText:[NSString stringWithFormat:#"Welcome %#!", [_user objectForKey:#"username"]]];
}];
}
- (IBAction)logout:(id)sender {
XYZAppDelegate* appDelegate = (XYZAppDelegate*)[[UIApplication sharedApplication] delegate];
[appDelegate logout];
}
XYZAppDelegate.m
- (void)login
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *identifier = [prefs stringForKey:#"accountidentifier"];
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
NSString *viewIdentifier = #"WelcomeView";
if(identifier != nil){
NXOAuth2Account *account = [[NXOAuth2AccountStore sharedStore] accountWithIdentifier:identifier];
if(account != nil) {
viewIdentifier = #"MainView";
}
UIViewController *controller = [mainStoryboard instantiateViewControllerWithIdentifier: viewIdentifier];
[navigationController pushViewController:controller animated:NO];
return;
}
}
- (void)logout
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs removeObjectForKey:#"accountidentifier"];
[prefs synchronize];
for (NXOAuth2Account *a in [[NXOAuth2AccountStore sharedStore] accounts] ){
[[NXOAuth2AccountStore sharedStore] removeAccount:a];
}
UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;
[navigationController popToRootViewControllerAnimated:YES];
}
I need to reinitialize all data in XYZMainViewController.
Thank you.
Look like problem is related to fetching JSON Object. It is possible that everytime you have send same user to fetch user data. You are not using NSUserdefault object to display name, you are using value, which is return by JSON Object. According to me cause of error is "withAccount:[XYZCommonFunctions user]" line.
I would like to suggest, instead of using
-(void)viewWillAppear:(BOOL)animated {
you can use
- (void)viewDidLoad
so that your login action performed only when your LoginController loads,instead when LoginController appear.
New viewwillAppear look as given below -
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.navigationController setNavigationBarHidden:YES animated:NO];
[self.label setText:#""];
}
and ViewDidLoad -
- (void)viewDidLoad
{
[super viewDidLoad];
[self homeProfile];
}
Also check your json response, whether you are getting response success or error.According to response need to handle.
Hope this helps.

Modal view controller not in the window hierarchy

The App I'm trying to do has a tabbar controller.
When the App starts, I'm getting the user location in the AppDelegate and when I've got the accuracy I need the AppDelegate sends an NSNotification to my App's starting page (index 0 of the tab bar controller).
Upon receiving the notification, this view tries to send an email with the user coordinates and other data, but as soon as the MFMailComposeViewController is presented I get the following error:
Warning: Attempt to present <MFMailComposeViewController: 0x98a0270> on <UITabBarController: 0x988c630> whose view is not in the window hierarchy!
What am I missing?
Thanks.
EDIT: adding some code...
This is what I've got in my AppDelegate.m:
- (void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSUserDefaults *phoneNumbers = [NSUserDefaults standardUserDefaults];
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 10.0) {
[self locationUpdate:newLocation];
smsLoc = newLocation;
if ([[phoneNumbers objectForKey:#"sendSMS"] isEqualToString:#"yes"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"sendSMS" object:nil];
} else if ([[phoneNumbers objectForKey:#"sendEmail"] isEqualToString:#"yes"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:#"sendEmail" object:nil];
}
}
}
Then, in my first view controller I have:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(sendSMS:) name:#"sendSMS" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(sendEmail:) name:#"sendEmail" object:nil];
}
And at the end, the selector for "sendSMS" (the other is pretty similar):
- (void)sendSMS: (NSNotification *)notification {
NSUserDefaults *phoneNumbers = [NSUserDefaults standardUserDefaults];
if ([phoneNumbers objectForKey:#"first"] || [phoneNumbers objectForKey:#"second"]) {
MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
if ([MFMessageComposeViewController canSendText]) {
AppDelegate *deleg = (AppDelegate *)[[UIApplication sharedApplication] delegate];
controller.body = [NSString stringWithFormat:#"some message with coordinates %.4f - %.4f", [deleg currentLocation].coordinate.latitude, [deleg currentLocation].coordinate.longitude];
controller.recipients = [NSArray arrayWithObjects:[phoneNumbers objectForKey:#"first"], [phoneNumbers objectForKey:#"second"], nil];
controller.messageComposeDelegate = self;
[self presentModalViewController:controller animated:YES];
}
}
}
}
Second edit: adding some more code.
UITabBarController *tabBarController = [[UITabBarController alloc] init];
tabBarController.delegate = self;
tabBarController.selectedIndex = 0;
[[tabBarController.tabBar.items objectAtIndex:0] setTitle:NSLocalizedString(#"Home", nil)];
[[tabBarController.tabBar.items objectAtIndex:1] setTitle:NSLocalizedString(#"Requests", nil)];
[[tabBarController.tabBar.items objectAtIndex:2] setTitle:NSLocalizedString(#"Account", nil)];
[[tabBarController.tabBar.items objectAtIndex:3] setTitle:NSLocalizedString(#"Settings", nil)];
//some other controls from DB
[[tabBarController.tabBar.items objectAtIndex:1] setBadgeValue:[NSString stringWithFormat:#"%d",number]];
The tabbarController has been made via IB, but I've added the code above in my AppDelegate because I need to localize the tab bar items and to add a badge to one of them.
Am I doing something wrong here?
I'm not sure if you have solve this issue. The error message means the viewcontroller you use to present another modal viewcontroller is not visible on the window. This can happen for e.g:
[VC1 presentModalViewController:VC2];
// Error here, since VC1 is no longer visible on the window
[VC1 presentModalViewController:VC3];
If your issue is like above, you can fix it like:
if (self.modalViewController != nil) {
[self.modalViewController presentModalViewController:VC3 animated:YES];
} else {
[self.tabBarController presentModalViewController:VC3 animated:YES];
}
If that doesn't fix your issue, maybe you can try to present using self.tabBarController instead of self. Again just suggestion, not sure if it works though.
Using this may help someone: [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:picker animated:NO completion:nil];
Since modalViewController and presentModalViewController are deprecated, the following is what works for me:
presentingVC = [[UIApplication sharedApplication] keyWindow].rootViewController;
if (presentingVC.presentedViewController) {
[presentingVC.presentedViewController presentViewController:VC3 animated:YES completion:nil];
} else {
[presentingVC presentViewController:VC3 animated:YES completion:nil];
}
You can follow this pattern
[VC1 presentModalViewController:VC2];
//
[**VC2** presentModalViewController:VC3];

Hiding ViewControllers after first launch

In my Application, I have a ViewController that loads up as a welcome screen. You tap "Go" then anther ViewController appears making the user create an account. when the information is submitted, their profile appears. (This is a fun test app) I want to make it so that when the user registers, uses their profile then quits the app, they don't have to keep re-registering. So I need help detecting the apps first launch, then making the WelcomeViewController and the RegisterViewController go away after first launch.
WelcomeViewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"hasLaunchedOnce"]) {
ProfileViewController *profileVC = [[ProfileViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:profileVC animated:NO completion:nil];
} else {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"hasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
RegisterViewController:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"hasLaunchedOnce"]) {
ProfileViewController *profileVC = [[ProfileViewController alloc] initWithNibName:nil bundle:nil];
[self presentViewController:profileVC animated:NO completion:nil];
} else {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:#"hasLaunchedOnce"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
}
NSUserDefaults is what you are looking for... store the state of the logged in user in NSUserDefaults and check/load it at the application startup...

Resources