-2

I am trying to call a method from a NSObject class from my AppDelegate. Usually this works if calling from a UIViewController but not having luck within the AppDelegate. My code:

AppDelegate.m

#import "ACManager.h"

@implementation AppDelegate {
    ACManager *acManager;
}

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[acManager login];
}

ACManager.h

@interface ACManager : NSObject

-(void)login;

@end

ACManager.m

+(ACManager*)sharedInstance {
    static ACManager *sharedInstance;
    @synchronized(self) {
        if (!sharedInstance) {
            sharedInstance = [[self alloc]init];
        }
    }
    return sharedInstance;
}

-(void)login
{
    NSLog(@"login run");
}

@end

Any ideas thank you. Is there a different way around this when calling from the app delegate?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
George99999
  • 53
  • 1
  • 9
  • Not relevant to the question, but you may want to replace your sharedInstance implementation with the one offered here. https://stackoverflow.com/questions/5720029/create-singleton-using-gcds-dispatch-once-in-objective-c – wjl May 20 '15 at 15:21

2 Answers2

5

For the singleton you need to use:

[[ACManager sharedInstance] login];

or assign value to your variable:

acManager=[ACManager sharedInstance];

and then, call:

[acManager login];
Nekak Kinich
  • 905
  • 7
  • 10
2

You are saying:

[acManager login];

Thus, you are sending an instance message to acManager. But acManager is nil! You have forgotten to supply an actual ACManager instance and place it in that slot (assign it to the variable).

Thus, nothing happens.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • what would be the solution to this? – George99999 May 20 '15 at 15:21
  • A basic understanding of variables and instances in Objective-C would be good. :) – matt May 20 '15 at 15:23
  • "what would be the solution to this?". But you said it works if calling from a view controller. I'm not being rude, but if you can get it to work from a view controller, but then can't get it to work from the delegate, and also need to ask how to get it to work, then you really really need to heed matt's advice and pause and do a bit of basic background reading on programming first. Good luck ;-) – Gruntcakes May 20 '15 at 15:29