Swift 无操作时自动登出
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 无操作时自动登出的更多相关文章
- secureCRT无操作自动登出时间修改(亲测可用)
转自:http://blog.sina.com.cn/s/blog_6bcf42010102vlt9.html secureCRT连接机器经常会因为一段时间无操作就退出了,提示timed out wa ...
- Andriod一段时间未操作页面,系统自动登出
功能描述: APP在公共的设备上运行,出于安全考虑,当登录的用户在超过一定时间内没有做任何操作, 则系统自动登出,用户如需重新操作APP,则需要重新登录 . 第一步:创建一个BaseActivity ...
- 两种方法实现Linux不活动用户登录超时后自动登出
在平常的工作中,windows系统使用的比较多一些,身边的很多同事都会对自己的电脑进行一些个性化设置,比如离开一段时间后自动锁屏.自动关闭屏幕.自动注销当前登录等.在windows下可以这样操作,那么 ...
- Linux下实现不活动用户登录超时后自动登出
方法一:通过修改.bashrc或.bash_profile文件来实现 通过修改home目录下的.bashrc或.bash_profile文件来实现.这两个文件选择其中一个在末尾加入如下一行,具体操作 ...
- Vue 页面15分钟无操作时返回首页
这种需求手机端和pc端一般是不存在的,因为都是可以手动操作刷新的. 最近在做一个户外社区大屏的项目,因为大屏是全屏显示,没法手动刷新,不可能在页面专门做一个刷新按钮,也不好看,那这样的需求就显得格外重 ...
- WPF 窗体中获取键盘和鼠标无操作时的超时提示
原文:WPF 窗体中获取键盘和鼠标无操作时的超时提示 通过调用Windows API中的GetLastInputInfo来获取最后一次输入的时间 , , ); timer.Tic ...
- ssh修改超时自动登出时间的方法
echo $TMOUT 查看当前服务器登出时间,如果没有输出表示不会登出 1.修改:vim ~/.bash_profile 2.设置TMOUT值 TMOUT=600 #表示10分钟之后自动登出 TMO ...
- 在 Linux 上实现一段时间后自动登出非活动用户
参考 编辑 ~/.bashrc 或 ~/.bash_profile 文件: $ vi ~/.bashrc 或, $ vi ~/.bash_profile 将下面行加入其中: TMOUT=100 这会让 ...
- ssh一段时间无操作后自动断开连接(假死)问题
平时使用ssh远程服务器的时候(注:远程虚拟机一般不会有这个问题),一段时间没有操作,ssh连接就会处于假死状态,以至于需要重新进行ssh连接,不管你用的什么远程工具都会出现这个问题,那么通过心跳检测 ...
随机推荐
- log4j2按日志级别输出到指定文件
在项目中,可能会产生非常多的日志记录,为了方便日志分析,一般可以将日志按级别输出到指定文件,本次就先说说log4j2的实现吧: 1.先加入log4j2依赖包 2.写一个java类进行测试,类文件中仅仅 ...
- 【转载】JExcelApi(JXL)学习笔记
在公司的项目中,有excel生成.导出的需求,因此学习了用JXL读写excel,做个简单的笔记,以供参考. 实现用java操作excel的工具,一般用的有两个:一个是JXL,另一个是apac ...
- iOS开发值得学习的Demo
一.HXWeiboPhotoPicker - 仿微博照片选择器 GitHub地址:https://github.com/LoveZYForever/HXWeiboPhotoPicker 二.AFNet ...
- MySQL修改max_allowed_packet
因mysql从库报错Last_IO_Error: Got a packet bigger than 'max_allowed_packet' bytes mysql> show slave st ...
- Android studio 2.3安装遇到的问题
我的安装系统环境:windows 10+jdk1.8.0_111 . 1.在安装Android studio 2.3之前,请安装最新的java jdk.Android studio的安装包里是不包含j ...
- 使用maven命令进行打包,部署项目到远程仓库
如果要部署项目到远程仓库, 方法一:配置pom.xml: <distributionManagement> <repository> <id>releases< ...
- R中的sub替换函数【转】
R中的grep.grepl.sub.gsub.regexpr.gregexpr等函数都使用正则表达式的规则进行匹配.默认是egrep的规则,也可以选用Perl语言的规则.在这里,我们以R中的sub函数 ...
- RNAseq 流程
https://github.com/twbattaglia/RNAseq-workflow
- webstorm的安装、激活码、更换主题颜色的修改、汉化
一.安装 1.解压webstorm11zh.rar,双击.exe文件,下一步安装,在安装结束前会提示输入激活码,这个从网上随便找一个可用的即可. 二.更换主题颜色: 1.先从网上找一个喜欢的主题颜色, ...
- [Spring]Spring Mvc实现国际化/多语言
1.添加多语言文件*.properties F64_en_EN.properties详情如下: F60_G00_M100=Please select data. F60_G00_M101=Are yo ...