19

I have created an Unity iOS App. The reason I have created the App on unity is because it can be easily ported to other platforms.

I am communicating with Axivity Sensors via BLE technology. Everything is working fine. But now I want to run the app in background. So for that I found that I should use UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification notifications so that I can do some background processing.

But sometimes I am not notified when App becomes active or deactive.

Is there anything that I am doing wrong or is there a better way to do that ?

Following is the code:

-(id) init {

    self = [super init];

    if (!self) return nil;

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillResignActive:) name:UIApplicationWillResignActiveNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBecomeActive:) name:UIApplicationDidBecomeActiveNotification object:nil];

        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appWillTerminate:) name:UIApplicationWillTerminateNotification object:nil];

        return self;
    }

-(void)appWillResignActive:(NSNotification*)note {

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(BLEOnCharactersticsUpdateNotification:)
                                             name:BLEOnCharactersticsUpdate object:nil]; 
}

-(void)appDidBecomeActive:(NSNotification*)note {

    NSLog(@"AppDidBecomeActive ");

    [[NSNotificationCenter defaultCenter] removeObserver:self name:BLEOnCharactersticsUpdate object:nil];

    for(int timeStampIndex = 0; timeStampIndex < [timeStampArray count]; timeStampIndex++) {

        NSLog(@"TimeStamp %i : Value : %@",timeStampIndex,[timeStampArray objectAtIndex:timeStampIndex]);
    }
}

-(void)appWillTerminate:(NSNotification*)note {

    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillTerminateNotification object:nil];
}
8
  • Which version of unity you are using ? Sep 19, 2016 at 11:37
  • @NeverHopeless Unity 5.4.0 f3 Sep 19, 2016 at 11:49
  • 1
    The snippet you posted here, is this the auto generated code taken from unity->xcode export or you wrote it yourself in xcode ? Sep 19, 2016 at 11:55
  • 1
    Did you find AppDelegate class in your project, these application functions are the part of that class if you are defining it somewhere else, first make sure it is being called in app delegate as per the expectation and then pass message from AppDelegate to your written code. Also, in unity we have facility to check background mode http://answers.unity3d.com/questions/948464/how-can-i-know-is-runinbackground.html can you accomplish your need from here ? Sep 19, 2016 at 12:18
  • 1
    A quick question, did you check the capabilities tab in your project setting and enable background mode? Also please check when it is expected and not calling active/inactive events, is it calling didfinishlaunching function ? For broken link you can check this link as well: answers.unity3d.com/questions/329615/… Sep 20, 2016 at 14:16

1 Answer 1

0

How do you guarantee that the object from the code sample exists during whole application lifecycle? Most likely, it's deallocated at some point of execution, that's why it doesn't receive the notifications. iOS itself guarantees that the app always receives at least UIApplicationDidBecomeActiveNotification and UIApplicationWillResignActiveNotification.

P.S.You subscribe for notifications in init method. And unsubscribe in appWillTerminate. A better place to unsubscribe would be dealloc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.