APIdleManager 是 iOS 中的一个空闲超时类。它允许自定义空闲超时功能,无需子类化 UIApplication 来处理应用的后台和活动实例。
pod 'ApIdleManager'
添加到 podfile 来使用 Cocoapods 安装 APIdleManager,或者您可以手动将 APIdleManager.h
和 APIdleManager.m
添加到项目中。实现 onTimeout
块以实例化空闲管理器,开始您的空闲倒计时,并在应用超时时执行必要的功能。如果没有任何块实现,则类将无法正常工作。
[APIdleManager sharedInstance].onTimeout = ^(void){ //Timeout implementation goes here };
确保将 kMaxIdleTimeSeconds
设置为您希望应用运行超时功能的秒数。该设置位于 APIdleManager 的头文件中。
// APIdleManager.h
#define kMaxIdleTimeSeconds 60.0
使用 [[APIdleManager sharedInstance] didReceiveInput];
来刷新您的计时器。为了拦截视图控制器中的任何触摸,覆盖 -(UIResponder *)nextResponder
方法
- (UIResponder *)nextResponder
{
[[APIdleManager sharedInstance] didReceiveInput];
//Any other previous functionality you had
return [super nextResponder];
}
如果您的应用进入后台,用户重新打开应用,请使用 [[RTLIdleManager sharedInstance] checkAndReset]
检查分配的空闲时间是否已通过。此功能的最佳默认方法在 App Delegate 中
- (void)applicationDidBecomeActive:(UIApplication *)application
{
[[RTLIdleManager sharedInstance] checkAndReset];
}
如果由于 willCreateNewTimerOnTimeout
默认设置为 YES
而超时,空闲管理器会始终创建一个新的计时器。如果您不希望有持续的空闲计时器或计划手动分配一个新的计时器,使用 -(void)createTimer
,则将此布尔值设置为 NO
。
示例可在示例应用程序中找到。祝您使用愉快。(
APIdleManager 在 MIT 许可下可用。有关更多信息,请参阅 LICENSE 文件。