当前位置: 首页 > news >正文

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>

相关文章:

  • SQL 死锁进程查看与处理
  • SQL Server进程阻塞的检查和解决办法
  • Azure 软件架构选择
  • Flex与.NET互操作(十二):FluorineFx.Net的及时通信应用(Remote Shared Objects)(三)
  • 简单说说什么是Restful
  • Flex与.NET互操作(十三):FluorineFx.Net实现视频录制与视频回放
  • Swift中的本地化实现
  • Azure .Net应用架构原型
  • Nebula3中的委托(Delegate)
  • 使用微软T4 template进行代码生成
  • ora-12537教训
  • Asp.Net MVC webAPI Token based authentication
  • Asp.Net 2.0 TreeView的Checkbox级联操作
  • azure最佳实践系列1-自我修复的设计
  • Nebula3 RTTI 小技巧
  • angular2开源库收集
  • Bootstrap JS插件Alert源码分析
  • IndexedDB
  • Linux gpio口使用方法
  • macOS 中 shell 创建文件夹及文件并 VS Code 打开
  • nginx 配置多 域名 + 多 https
  • Redis在Web项目中的应用与实践
  • Spring声明式事务管理之一:五大属性分析
  • Vue全家桶实现一个Web App
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 彻底搞懂浏览器Event-loop
  • 翻译:Hystrix - How To Use
  • 飞驰在Mesos的涡轮引擎上
  • 跨域
  • 巧用 TypeScript (一)
  • 如何设计一个比特币钱包服务
  • #include
  • #pragma pack(1)
  • (Java实习生)每日10道面试题打卡——JavaWeb篇
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (介绍与使用)物联网NodeMCUESP8266(ESP-12F)连接新版onenet mqtt协议实现上传数据(温湿度)和下发指令(控制LED灯)
  • (全注解开发)学习Spring-MVC的第三天
  • .Family_物联网
  • .NET Standard 的管理策略
  • .Net面试题4
  • .NET企业级应用架构设计系列之技术选型
  • .NET中GET与SET的用法
  • @private @protected @public
  • [ web基础篇 ] Burp Suite 爆破 Basic 认证密码
  • [ACTF2020 新生赛]Upload 1
  • [AutoSar]状态管理(五)Dcm与BswM、EcuM的复位实现
  • [cocos creator]EditBox,editing-return事件,清空输入框
  • [ERROR ImagePull]: failed to pull image k8s.gcr.io/kube-controller-manager失败
  • [HNOI2008]玩具装箱toy
  • [Linux基础开发工具---vim]关于vim的介绍、vim如何配置及vim的基本操作方法
  • [NodeJS]NodeJS基于WebSocket的多用户点对点即时通讯聊天
  • [NOIP 2015] Day.1 T1 神奇的幻方 [模拟]
  • [NSSCTF]-Web:[SWPUCTF 2021 新生赛]easyrce解析
  • [PHP] 代码重用与函数