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. 20145327 《Java程序设计》第六周学习总结

    20145327 <Java程序设计>第六周学习总结 教材学习内容总结 父类中的方法: 流(Stream)是对「输入输出」的抽象,而「输入输出」是相对程序而言的. 标准输入输出: Syst ...

  2. 20144303 《Java程序设计》第五周学习总结

    20144303 <Java程序设计>第五周学习总结 教材学习内容总结 第八章 异常处理 异常就是程序在运行时出现不正常情况,异常的由来是因为Java把出现的问题封装成了对象,换句话说Ja ...

  3. 解读:MultipleOutputs类

    //MultipleOutputs类用于简化多文件输出The MultipleOutputs class simplifies writing output data to multiple outp ...

  4. Spring Boot 之restful风格

    步骤一:restful风格是什么? 我们知道在做web开发的过程中,method常用的值是get和post.可事实上,method值还可以是put和delete等等其他值. 既然method值如此丰富 ...

  5. 2017 beijing icpc A - Euler theorem

    2017-09-22 21:59:43 writer:pprp HazelFan is given two positive integers a,ba,b, and he wants to calc ...

  6. 解题报告:hdu1008 Elvator

    2017-09-07 19:30:22 writer:pprp 比较顺利,最近生活出现了各种问题, 发生了很多矛盾,我要耐下心来,最重要的不是努力不努力,而是选择 希望我能处理好人际关系还有学业上的压 ...

  7. hdu5616 暴力枚举

    2017-08-25 20:08:54 writer:pprp 题目简述: • HDU 5616• n个砝码,可以放在天平左右两侧或不放• m次询问,每次询问是否可以测出给定重量• 1 ≤ n ≤ 2 ...

  8. Gray Code,求格林码

    问题描述: The gray code is a binary numeral system where two successive values differ in only one bit. G ...

  9. 机器学习经典书籍&论文

    原文地址:http://blog.sina.com.cn/s/blog_7e5f32ff0102vlgj.html 入门书单 1.<数学之美>PDF6 作者吴军大家都很熟悉.以极为通俗的语 ...

  10. 【Jmeter】压测mysql数据库中间件mycat

    背景 因为博主所负责测试的项目需要数据库有较大的吞吐量,在最近进行了升级,更新了一个数据库中间件 - - mycat.查询了一些资料,了解到这是阿里的一个开源项目,基于mysql,是针对磁盘的读与写, ...