Identify that an App is entering the background

0

I have an app that is in Object-C and I'm trying to pass it to Swift, but I'm having some problems, one of them is NSNotification that is not working.

In object-C, I'm using it as follows:

- (void)viewDidLoad{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidEnterBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [super viewDidLoad];
}

- (void) applicationDidEnterBackground:(NSNotification*)notification {
    NSLog(@"Entro em back");
}

In swift I'm trying the following way, but it's not calling the function:

override func viewDidLoad() {
    super.viewDidLoad()
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
 }

func applicationDidEnterBackground() {
    println("Entro em back")
}

In my case, I need to load an information into NSUserDefaults when the app loads and saves the information changed by the user when the app goes into the background.

Note: I can not access either function.

    
asked by anonymous 25.03.2015 / 02:58

1 answer

0

In fact, your code for one detail is wrong.

Notice that the applicationDidEnterBackground: method has an argument and just like in Objective-C, from the moment your method has arguments, you should mention them in the selector separated by : to sign the selector correctly, so your code should look like this:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationDidEnterBackground:", name: UIApplicationDidEnterBackgroundNotification, object: nil)

Notice the: at the end of the method signature.

I hope I have helped.

    
09.04.2015 / 05:57