微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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),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>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐