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. MR案例:定制InputFormat

    数据输入格式 InputFormat类用于描述MR作业的输入规范,主要功能:输入规范检查(比如输入文件目录的检查).对数据文件进行输入切分和从输入分块中将数据记录逐一读取出来.并转化为Map的输入键值 ...

  2. Java GC垃圾回收

    Java的内存分配和回收也主要在Java的堆上进行的,Java的堆中存储了大量的对象实例,所以Java的堆也叫GC堆. Java在垃圾收集的过程中,主要用到了分代收集算法,具体有复制.标记清除.标记压 ...

  3. CentOS安装wkhtmltopdf及解决中文支持问题

    安装wkhtmltopdf,先下载  wkhtmltox-0.12.2.1_linux-centos6-amd64.rpm yum install -y wkhtmltox-0.12.2.1_linu ...

  4. 在php中define和const定义常量的区别

    define和const都可以用来定义常量,但是const定义常量的时候大小写敏感,而define可以通过设置第三个参数为true的时候来取消大小写敏感! 如图: 引用地址:点这里

  5. zpar使用方法之Chinese Word Segmentation

    第一步在这里: http://people.sutd.edu.sg/~yue_zhang/doc/doc/qs.html 你可以找到这句话, 所以在命令行中分别敲入 make zpar make zp ...

  6. background-size css background-images

    在设计网页时,经常会用到背景图片来达到视觉效果. 一般情况下用repeat的方式是最适全不过了,不过有时间是采用整图来充当背景,那么这个时候就会有多种可能性的存在了. 整图来做背景一般是采用no-re ...

  7. IDEA.环境

    1.下载: https://www.jetbrains.com/idea/download/#section=windows 安装的版本是:Ultimate:ideaIU-2017.3.3.exe 2 ...

  8. GTS--阿里巴巴分布式事务全新解决方案

    现代IT应用中,服务化SOA作为主流的技术架构被广泛应用到各种信息系统.原来一个系统被分拆成若干个服务的集合,产生了跨服务调用的分布式事务问题.随着Dubbo.SpringCloud等微服务框架的流行 ...

  9. 英语每日阅读---4、VOA慢速英语(翻译+字幕+讲解):专家:城市发展将加剧住房危机

    英语每日阅读---4.VOA慢速英语(翻译+字幕+讲解):专家:城市发展将加剧住房危机 一.总结 一句话总结:城市化(越来越多的人会住进城市)是必然趋势,人口增长也是必然趋势,人口增长必然会加大住房危 ...

  10. js今日小结—Ajax、前端安全、GET&POST、闭包、HTTPS

    HTTPS HTTP+加密(SSL.TLS)+认证+完整性保护 = HTTPS: GET和POST的区别 get拉取数据,post传输数据 get请求能被浏览器主动缓存,post不会(除非手动) ge ...