最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中实现app自动登录

Swift crifan 3972浏览 0评论

swift auto login

Automatic Login in IOS Swift – Stack Overflow

iphone – iOS: How to authenticate a user after login (for autologin)? – Stack Overflow

【总结】

基本逻辑是:

1.用户正常登录后,保存对应的用户信息:

用户名和密码

注意不要以NSUserDefaults的形势保存-》因为是明文保存

而要用钥匙链的方式(此处用的是KeychainWrapper.swift)保存-》是加密保存,更安全

2.还是正常的先进入登录页面

3.尝试去恢复用户信息,如果恢复成功,则自动去登录

4.如果恢复失败,则不做任何动作-》等用户自己点击登录按钮去登录

5.如果是第一次启动app,则不要自动登录

相关代码如下:

/Users/crifan/dev/dev_root/daryun/JianDao/iOS-Client/JianDao/CrifanLib.swift

import UIKit
func isFirstRunApp() -> Bool {
    var isFirstRun = false
    
    let keyHasLaunchedBefore = "hasLaunchedBefore"
    
    let hasLaunchedBefore = NSUserDefaults.standardUserDefaults().boolForKey(keyHasLaunchedBefore)
    if hasLaunchedBefore  {
        isFirstRun = false
    }
    else {
        isFirstRun = true
        NSUserDefaults.standardUserDefaults().setBool(true, forKey: keyHasLaunchedBefore)
    }
    
    return isFirstRun
}

/Users/crifan/dev/dev_root/daryun/JianDao/iOS-Client/JianDao/JianDao/AppDelegate.swift

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        if isFirstRunApp() {
            gCurUserItem.needAutoLogin = false
            
            showLoginView()
            
            NSThread.sleepForTimeInterval(2)
        } else {
            gCurUserItem.needAutoLogin = false
            
            SingletonLoginVC().tryRestoreUserAndPassword()
            SingletonLoginVC().loginAction()
            dispatch_semaphore_wait(loginDoneSemaphore, DISPATCH_TIME_FOREVER)
            
            if gCurUserItem.hasLogined {
                gLog.info("login successfully")
                //switch to Main View Controller
                resetMainVC()
                showMainView()
            } else {
                gLog.debug("login failed, so show login view")
                showLoginView()
            }
        }
}

/Users/crifan/dev/dev_root/daryun/JianDao/iOS-Client/JianDao/LoginViewController.swift

    func tryRestoreUserAndPassword() {
        gLog.debug("")
        var restoreOk = false
        var restoredUser:String? = nil
        var restoredPassword:String? = nil
        (restoreOk, restoredUser, restoredPassword) = restoreUserAndPassword()
        if restoreOk {
            userTextField.text = restoredUser
            passwordTextField.text = restoredPassword
        }
    }

/Users/crifan/dev/dev_root/daryun/JianDao/iOS-Client/JianDao/GlobalFunctions.swift

import UIKit
func storeUserAndPassword(user:String, password:String){
    //method 1: store in plain text
    //NSUserDefaults.standardUserDefaults().setValue(user, forKey: StrUserKey)
    //NSUserDefaults.standardUserDefaults().setValue(password, forKey: StrPasswordKey)
    
    //method 2: store to encryption info
    if !user.isEmpty {
        let saveUserOk: Bool = KeychainWrapper.setString(user, forKey: StrUserKey)
        gLog.info("save user \(user) \(saveUserOk)")
    }
    
    if !password.isEmpty{
        let savePasswordOk: Bool = KeychainWrapper.setString(password, forKey: StrPasswordKey)
        gLog.info("save password \(savePasswordOk)")
    }
}
func restoreUserAndPassword() -> (Bool, String?, String?) {
    var restoreOk = false
    var prevStoredUser:String?
    var prevStotedPassword:String?
    
    //        //method 1: get previous stored in plain text
    //        let prevStoredUser = NSUserDefaults.standardUserDefaults().stringForKey(StrUserKey)
    //        let prevStoredPassword = NSUserDefaults.standardUserDefaults().stringForKey(StrPasswordKey)
    
    //method 2: get previous stored in encryption
    prevStoredUser = KeychainWrapper.stringForKey(StrUserKey)
    prevStotedPassword = KeychainWrapper.stringForKey(StrPasswordKey)
    
    if (prevStoredUser != nil) &&
        !(prevStoredUser!.isEmpty) &&
        (prevStotedPassword != nil) &&
        !(prevStotedPassword!.isEmpty) {
        restoreOk = true
    }
    gLog.info("restore user and password: \(restoreOk), restored user: \(prevStoredUser)")
    return (restoreOk, prevStoredUser, prevStotedPassword)
}
func deletePrevStoredUserAndPassword(){
    let removeUserOk: Bool = KeychainWrapper.removeObjectForKey(StrUserKey)
    let removePasswordOk: Bool = KeychainWrapper.removeObjectForKey(StrPasswordKey)
    gLog.info("remove previous stored user: \(removeUserOk), remove previous stored password=\(removePasswordOk)")
}

转载请注明:在路上 » [已解决]swift中实现app自动登录

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.187 seconds, using 22.01MB memory