[英]How to detect the idle time/inactivity time for a particular view controller in 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, self.view.frame.size.width, 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 分鍾后,您可以將用戶重定向到任何您想要的位置。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.