ios - 无法在 iPhone 中获取密码 UItextfield,但在模拟器中可以

标签 ios iphone swift

我正在 xcode swift4 上开发一个应用程序。它是 shibboleth 的登录应用程序。当我在模拟器上运行应用程序时,它工作正常。然而,当我在 iphone6plus 上尝试时,我调用 UI 密码字段的功能它不接受它。 它只是跳过它(这意味着它不接受我在密码字段中写入的内容)并传递它,然后退出该函数。知道为什么吗? 谢谢

import UIKit
import SwiftECP
import XCGLogger
class ViewController: UIViewController {

@IBOutlet var UsernameField: UITextField!

@IBOutlet var passwordField: UITextField!


  var file = "file"

override func viewDidLoad() {
    super.viewDidLoad()
    if #available(iOS 10, *) {
        // Disables the password autoFill accessory view.
        UsernameField.textContentType = UITextContentType("")
        passwordField.textContentType = UITextContentType("")
    }
}

@IBAction func _Login(_ sender: Any) {
    gotourl()


    let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

    let fileURL = DocumentDirURL.appendingPathComponent("file").appendingPathExtension("txt")
    var readString = ""
    do {
        // Read the file contents
        readString = try String(contentsOf: fileURL)
//            print ( "Reading from file \(readString)")
    } catch let error as NSError {
        print("Failed reading from URL: \(fileURL), Error: " + error.localizedDescription)
    }

    let attributes = try! FileManager.default.attributesOfItem(atPath:fileURL.path)
    let fileSize = attributes[.size] as! NSNumber
 //        print ("Here is file \(fileSize)")


    if fileSize != 0{
        performSegue(withIdentifier: "gotowelcome", sender: self)

    }
    else
    {
        let alert = UIAlertController(title: "Login error", message: "Wrong Username or password.", preferredStyle: UIAlertControllerStyle.alert)

        // add an action (button)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.destructive, handler: { action in

            self.UsernameField.text=""
            self.passwordField.text=""

            }))

        // show the alert
        self.present(alert, animated: true, completion: nil)
        // Do any additional setup after loading the view.
        let text = ""
        do {
            try text.write(to: fileURL, atomically: false, encoding: .utf8)
            let fileSize2 = attributes[.size] as! NSNumber

        } catch {
            print(error)
        }
    }

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func gotourl(){
    let username: String = UsernameField.text!
    let password: String = passwordField.text!
    let protectedURL = URL(
        string: "https://itsapps.odu.edu/auth/getInfo.php"
        )!
    let logger = XCGLogger()
    logger.setup(level: .debug)

    ECPLogin(
        protectedURL: protectedURL,
        username: username,
        password: password,
        logger: logger
        ).start { event in
            switch event {

            case let .value( body) :
                // If the request was successful, the protected resource will
                // be available in 'body'. Make sure to implement a mechanism to
                // detect authorization timeouts.

                print("Response body: \(body)")

                //this is the file. we will write to and read from it

                let text = "\(body)" //just a text


                let DocumentDirURL = try! FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)

                let fileURL = DocumentDirURL.appendingPathComponent("file").appendingPathExtension("txt")
                print("FilePath: \(fileURL.path)")


                do {
                    // Write to the file
                    try text.write(to: fileURL, atomically: true, encoding: String.Encoding.utf8)
                } catch let error as NSError {
                    print("Failed writing to URL: \(fileURL), Error: " + error.localizedDescription)
                }
                // The Shibboleth auth cookie is now stored in the sharedHTTPCookieStorage.
                // Attach this cookie to subsequent requests to protected resources.
                // You can access the cookie with the following code:
                if let cookies = HTTPCookieStorage.shared.cookies {
                    let shibCookie = cookies.filter { (cookie: HTTPCookie) in
                        cookie.name.range(of: "shibsession") != nil
                        }[0]
                    print(shibCookie)
                }

            case let .failed(error):
                // This is an AnyError that wraps the error thrown.
                // This can help diagnose problems with your SP, your IdP, or even this library :)

                switch error.cause {
                case let ecpError as ECPError:
                    // Error with ECP
                    // User-friendly error message
                    print(ecpError.userMessage)

                    // Technical/debug error message
                    print(ecpError.description)
                case let alamofireRACError as AlamofireRACError:
                    // Error with the networking layer
                    print(alamofireRACError.description)
                default:
                    print("Unknown error!")
                    print(error)

                }

            default:
                break



            }
    }

}

}

在这部分中,由于跳过函数的其余部分而导致错误的原因

    @IBAction func _Login(_ sender: Any) {
    gotourl()

然后转到

  func gotourl(){
    let username: String = UsernameField.text!
    let password: String = passwordField.text!
    let protectedURL = URL(
        string: "https://itsapps.odu.edu/auth/getInfo.php"
        )!

并跳过密码:String = passwordField.text!

最佳答案

尝试以编程方式执行此操作。

设置txtfld.isSecureTextEntry = true 在 viewDidLoad 方法中。

关于ios - 无法在 iPhone 中获取密码 UItextfield,但在模拟器中可以,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49664623/

相关文章:

ios - 如何让 Collection View 中的 ImageView 根据屏幕尺寸自行调整大小?

iOS SpreadsheetView,旋转时切纸

iphone - MFMailComposeViewController 挂起我的应用程序

ios - 来自指针的 PFFile 抛出异常

ios - 使用 Facebook 好友填充桌面 View

ios - 在 iOS 中有一段代码等待在特定时间运行

iphone - iOS 是否支持通过 SMB 进行文件操作?

用于更快加载图像的 iOS Swift 技巧

iOS 截图/App 录制技巧

iphone - -[__NSCFArray objectAtIndex :]: index (1) beyond bounds (1)