如何为iOS中的特定视图控制器检测空闲时间/非活动时间?

问题描述

由于我是移动应用程序开发和Stack Overflow的新手,所以我需要进行个人培训以检测应用程序特定视图控制器中的空闲时间。实际上,我正在尝试在WKWebview中加载Web URL。因此,如果用户在此屏幕上花费了大约3分钟的空闲时间,则我的应用程序应该注销并进入主屏幕。是的,我已经研究了几个SO问题,但是所有这些问题都为整个应用程序实现了一个公共的空闲时间,但就我而言,我只想要此特定的视图控制器。这是我的视图控制器代码,用于在WKWebview中显示Web URL:

#import "WebViewController.h"
#import <WebKit/WebKit.h>

@interface WebViewController ()<WKNavigationDelegate,WKUIDelegate>{
WKWebView *_konyWebLoader;
}

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _konyWebLoader = [[WKWebView alloc] initWithFrame:CGRectMake(0,[UIApplication sharedApplication].statusBarFrame.size.height + 50,self.view.frame.size.width,self.view.frame.size.height -([UIApplication sharedApplication].statusBarFrame.size.height+70))]; 
    [_konyWebLoader setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
    NSURL *url = [NSURL URLWithString: self.productURL];
    NSURLRequest *urlReq = [NSURLRequest requestWithURL:url];
    NSLog(@"Product URL%@",_productURL);
    NSLog(@"TItle%@",_titleName);
    [_konyWebLoader loadRequest:urlReq];
    [self.view addSubview:_konyWebLoader];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // dispose of any resources that can be recreated.
}

-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UINavigationBar *myNav = [[UINavigationBar alloc]initWithFrame:CGRectMake(0,[UIApplication sharedApplication].statusBarFrame.size.height,50)];
    [[UINavigationBar appearance] setTitleTextAttributes:
    [NSDictionary dictionaryWithObjectsAndKeys:
     [UIColor whiteColor],NSForegroundColorAttributeName,[UIFont fontWithName:@"ArialMT" size:16.0],NSFontAttributeName,nil]];
    [myNav setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
   //[UINavigationBar appearance].barTintColor = [UIColor whiteColor];
    [UINavigationBar appearance].barTintColor = [UIColor colorWithRed: 0.40 green: 0.16 blue: 0.51 alpha: 1.00];
    
    [self.view addSubview:myNav];

    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                   style:UIBarButtonItemStyleBordered
                                                                  target:self
                                                                  action:@selector(backButtonClicked)];

    UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:_titleName];
    
    navigItem.leftBarButtonItem = cancelItem;
    myNav.items = [NSArray arrayWithObjects: navigItem,nil];

    [UIBarButtonItem appearance].tintColor = [UIColor whiteColor];
}

- (void)backButtonClicked {
    [self dismissViewControllerAnimated:YES completion:nil];
}
    
/*
#pragma mark - Navigation

// In a storyboard-based application,you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end

因此,如何只检查此特定视图控制器的空闲/不活动时间,并让我知道任何帮助。

解决方法

您可以使用NSTimer并在该特定视图控制器上注册TapGesture,当NSTimer方法调用3分钟后,您可以将用户重定向到任何位置。