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];
}
http://answers.unity3d.com/questions/948464/how-can-i-know-is-runinbackground.html
can you accomplish your need from here ?