2

Since I am very new to the Mobile Application development and Stack Overflow, I have a requirement for my personal training to detect the idle time in a particular view controller of my application. I am loading a web URL in a WKWebview. So if the user spends idle time on this screen for around 3 minutes my application should logoff and come to the home screen. Went through several SO questions but all those implements a common idle time for whole application but in my case, I want only for this particular view controller. This is my view controller code for showing web URL in WKWebview:

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

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

@end

@implementation WebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _wkViewer = [[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))]; 
    [_wkViewer setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
    NSURL *url = [NSURL URLWithString: self.productURL];
    NSURLRequest *urlReq = [NSURLRequest requestWithURL:url
    [_wkViewer loadRequest:urlReq];
    [self.view addSubview:_wkViewer];   
}
 
-(void) viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    UINavigationBar *navBar = [[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:@"Arial" size:20.0], NSFontAttributeName,nil]];
    [navBar setAutoresizingMask: UIViewAutoresizingFlexibleWidth];
    [UINavigationBar appearance].barTintColor = [UIColor whiteColor];
    [self.view addSubview: navBar];
    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backClicked)]                                                                                                                                                                                     
    UINavigationItem *navigItem = [[UINavigationItem alloc] initWithTitle:_titleName];
    navigItem.leftBarButtonItem = cancelItem;
    navBar.items = [NSArray arrayWithObjects: navigItem,nil];
    [UIBarButtonItem appearance].tintColor = [UIColor whiteColor];
}

- (void)backClicked {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
user16780334
  • 422
  • 5
  • 20
  • use NSTimer to track user screen. https://stackoverflow.com/questions/1449035/how-do-i-use-nstimer –  Sep 15 '20 at 04:19
  • @RB1509 thanks for the link but how to tell the user has touched the screen again ? If possible can you please help me the full working code in my answer? – Deborah cowley Sep 15 '20 at 04:22
  • i am currently work in swift i almost forgot objective c context so i will only help via link other wise wait ... i will update i after my office work will finish. –  Sep 15 '20 at 05:03
  • till just read uitouch document and nstimer you will get some ideas definitely. –  Sep 15 '20 at 05:04
  • @RB1509 thanks and i am ready to wait as i need objective c code – Deborah cowley Sep 15 '20 at 07:19
  • https://stackoverflow.com/questions/15383782/how-to-reset-nstimer-for-touch-touch-move-in-ios please see this it will work for you. –  Sep 15 '20 at 07:26
  • use touchesBegan: and touchesEnded: this two events when touch begin reset your timer and start on ended –  Sep 15 '20 at 07:30
  • @RB1509 thanks again... if you please post a full working code then i can support your answer – Deborah cowley Sep 15 '20 at 09:06

1 Answers1

1

You can use NSTimer and Register the TapGesture on that specific view controller,When the NSTimer Method called After 3 minutes, you can redirect the user wherever you want.

shafi
  • 426
  • 3
  • 15
  • but for this we have to tap on screen. It will not work when we just simple tough the screen. How to handle touch screen scenario? – Deborah cowley Sep 16 '20 at 07:03
  • 1
    You have to invalidate the timer instance, And reinitialized the timer again, so that when the user taps on the screen, you start your timer again – shafi Sep 16 '20 at 12:24