【发布时间】:2017-01-26 09:04:59
【问题描述】:
我创建了一个 Unity iOS 应用程序。我在 unity 上创建 App 的原因是因为它可以轻松移植到其他平台。
我正在通过 BLE 技术与Axivity Sensors 通信。一切正常。但现在我想在后台运行应用程序。因此,我发现我应该使用UIApplicationDidBecomeActiveNotification 和UIApplicationWillResignActiveNotification 通知,以便我可以进行一些后台处理。
但有时我不会在应用变为活动或不活动时收到通知。
我做错了什么或者有更好的方法吗?
以下是代码:
-(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];
}
【问题讨论】:
-
您使用的是哪个版本的统一?
-
@NeverHopeless Unity 5.4.0 f3
-
您在此处发布的 sn-p,这是从 unity->xcode 导出自动生成的代码还是您自己在 xcode 中编写的?
-
您是否在项目中找到 AppDelegate 类,如果您在其他地方定义它,这些应用程序函数是该类的一部分,首先确保它按照预期在应用程序委托中被调用,然后将消息从 AppDelegate 传递到您编写的代码。另外,在统一中,我们可以检查后台模式
http://answers.unity3d.com/questions/948464/how-can-i-know-is-runinbackground.html您可以从这里完成您的需求吗? -
一个简单的问题,您是否检查了项目设置中的功能选项卡并启用了后台模式?另外请检查它何时是预期的,而不是调用活动/非活动事件,它是否调用了 didfinishlaunching 函数?对于断开的链接,您也可以查看此链接:answers.unity3d.com/questions/329615/…
标签: ios objective-c xcode unity3d