加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 无操作时自动登出

发布时间:2020-12-14 06:11:37 所属栏目:百科 来源:网络整理
导读:main.swift中代码: import Foundationimport UIKitUIApplicationMain( CommandLine.argc,UnsafeMutableRawPointer(CommandLine.unsafeArgv) .bindMemory( to: UnsafeMutablePointerInt8.self,capacity: Int(CommandLine.argc)),NSStringFromClass(MyApplica

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>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读