main.swift中代码:

import Foundation
import UIKit

UIApplicationMain(
    CommandLine.argc,
    UnsafeMutableRawPointer(CommandLine.unsafeArgv)
        .bindMemory(
            to: UnsafeMutablePointer<Int8>.self,
            capacity: Int(CommandLine.argc)),
    NSStringFromClass(MyApplication.self),
    NSStringFromClass(AppDelegate.self)
)

UIApplication.swift
import Foundation
import UIKit

@objc(MyApplication)

class MyApplication: UIApplication {

    override init(){
        super.init();

        if(_timer == nil){
            _timer = Timer.scheduledTimer(
                timeInterval: TimeInterval(_idleSecondsTriggerLogout), target: self, selector: #selector(MyApplication.dosomething), userInfo: nil, repeats: true)
        }
    }

    let _idleSecondsTriggerLogout:Int = 8 * 60; // 8 mins, same with android
    var _timer:Timer? = nil;

    override func sendEvent(_ event: UIEvent) {

        // Ignore .Motion and .RemoteControl event simply everything else then .Touches
        if event.type != .touches {
            super.sendEvent(event)
            return
        }

        // .Touches only
        var restartTimer = true
        if let touches = event.allTouches {
            // At least one touch in progress? Do not restart timer, just invalidate it
            for touch in touches.enumerated() {
                if touch.element.phase != .cancelled && touch.element.phase != .ended {
                    restartTimer = false
                    break
                }
            }
        }

        if restartTimer {
            // Touches ended || cancelled, restart timer
            resetTimer()
        } else {
            // Touches in progress - !ended, !cancelled, just invalidate it
            //print("Touches in progress. Invalidate timer")
        }

        super.sendEvent(event)
    }

    func resetTimer() {
        _timer?.invalidate()
        _timer = Timer.scheduledTimer(
            timeInterval: TimeInterval(_idleSecondsTriggerLogout), target: self, selector: #selector(MyApplication.dosomething), userInfo: nil, repeats: true)
    }
    func dosomething(){
        print("time up, logout and clear session 1");
        ServiceProxy.HttpMeta.RefreshToken = "";
        ServiceProxy.HttpMeta.Token = "";
        ServiceProxy.HttpMeta.UserName = "";
        print("time up, logout and clear session 2");

        let topVC = topViewController()

        print("time up, logout and clear session 3");
        let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
        print("time up, logout and clear session 4");
        let nextViewController = storyBoard.instantiateViewController(withIdentifier: "LoginVC") as! LoginVC
        print("time up, logout and clear session 5");
        topVC?.present(nextViewController, animated:true, completion:nil)
        print("time up, logout and clear session 6");
    }

    func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        return controller
    }

}

AppDelegate.swift中代码(注释掉UIApplicationMain)

//@UIApplicationMain

info.plist中代码:

    <key>Principal class</key>
    <string>MyApplication</string>

Swift 无操作时自动登出的更多相关文章

  1. secureCRT无操作自动登出时间修改(亲测可用)

    转自:http://blog.sina.com.cn/s/blog_6bcf42010102vlt9.html secureCRT连接机器经常会因为一段时间无操作就退出了,提示timed out wa ...

  2. Andriod一段时间未操作页面,系统自动登出

    功能描述: APP在公共的设备上运行,出于安全考虑,当登录的用户在超过一定时间内没有做任何操作, 则系统自动登出,用户如需重新操作APP,则需要重新登录 . 第一步:创建一个BaseActivity ...

  3. 两种方法实现Linux不活动用户登录超时后自动登出

    在平常的工作中,windows系统使用的比较多一些,身边的很多同事都会对自己的电脑进行一些个性化设置,比如离开一段时间后自动锁屏.自动关闭屏幕.自动注销当前登录等.在windows下可以这样操作,那么 ...

  4. Linux下实现不活动用户登录超时后自动登出

    方法一:通过修改.bashrc或.bash_profile文件来实现  通过修改home目录下的.bashrc或.bash_profile文件来实现.这两个文件选择其中一个在末尾加入如下一行,具体操作 ...

  5. Vue 页面15分钟无操作时返回首页

    这种需求手机端和pc端一般是不存在的,因为都是可以手动操作刷新的. 最近在做一个户外社区大屏的项目,因为大屏是全屏显示,没法手动刷新,不可能在页面专门做一个刷新按钮,也不好看,那这样的需求就显得格外重 ...

  6. WPF 窗体中获取键盘和鼠标无操作时的超时提示

    原文:WPF 窗体中获取键盘和鼠标无操作时的超时提示 通过调用Windows API中的GetLastInputInfo来获取最后一次输入的时间 , , );            timer.Tic ...

  7. ssh修改超时自动登出时间的方法

    echo $TMOUT 查看当前服务器登出时间,如果没有输出表示不会登出 1.修改:vim ~/.bash_profile 2.设置TMOUT值 TMOUT=600 #表示10分钟之后自动登出 TMO ...

  8. 在 Linux 上实现一段时间后自动登出非活动用户

    参考 编辑 ~/.bashrc 或 ~/.bash_profile 文件: $ vi ~/.bashrc 或, $ vi ~/.bash_profile 将下面行加入其中: TMOUT=100 这会让 ...

  9. ssh一段时间无操作后自动断开连接(假死)问题

    平时使用ssh远程服务器的时候(注:远程虚拟机一般不会有这个问题),一段时间没有操作,ssh连接就会处于假死状态,以至于需要重新进行ssh连接,不管你用的什么远程工具都会出现这个问题,那么通过心跳检测 ...

随机推荐

  1. Job流程:Shuffle详解

    此文承接Job流程:Mapper类分析.MapReduce为确保每个reducer的输入都按键排序,数据从map输出到reducer输入的这段过程成为Shuffle. map端 1).Spill溢写. ...

  2. zip unzip tar 压缩解压

    yum install -y unzip zip    yum安装zip -r mydata.zip mydata    mydata目录压缩为mydata.zipunzip mydata.zip - ...

  3. 永久更改hostname主机名

    vim /etc/sysconfig/network NETWORKING=yesHOSTNAME=keepalived-nginx1GATEWAY=192.168.33.2 vim /etc/hos ...

  4. webservice的SOAP代理设置

    我们的服务器是可以访问的 设置的代理同样也可以访问 接下来设置代理 接着打开TCP窗口 最后来看数据

  5. springboot idea 配置热加载

    在idea 配置springboot的热加载,只需要三步: 第一步.引用jar包 <dependency> <groupId>org.springframework.boot& ...

  6. mysql 跨库查询问题

    MySQL实现跨服务器查询 https://blog.csdn.net/LYK_for_dba/article/details/78180444 mysql> create database l ...

  7. SQL生成两个时间之间的所有日期

    select dateadd(dd,number,'2012-1-1') AS date from master..spt_values where type='p' and dateadd(dd,n ...

  8. SHOW INNODB STATUS 探秘

    [InnoDB系列] -- SHOW INNODB STATUS 探秘 SHOW INNODB STATUS 探秘 转载:http://imysql.com/2008_05_22_walk_throu ...

  9. 【hive】在alter修改元数据的时候报错 mismatched input 'xxxxx' expecting KW_EXCHANGE

    目的:修改表某个字段属性 语句: 报错信息 错误原因: 在HiveQL中,alter命令不使用与create或select相同的语义 ; 具体来说,您不能使用“ALTER DATABASE.TABLE ...

  10. 为什么是link-visited-hover-active

    前言 通常我们在设置链接的一些伪类(link,visited,hover,active)样式时,要让不同的状态显示正确的样式,我们需要按一定的顺序设置这些伪类的样式.这里我就按CSS2规范中推荐的顺序 ...