Canot在Iphone中获取密码UItextfield,但它在模拟器中

时间:2018-04-05 05:17:27

标签: ios iphone swift

我正在xcode swift4上开发一个应用程序。它是shibboleth的登录应用程序。当我在模拟器上运行应用程序时它工作正常。但是,当我尝试iphone6plus时,我调用UI密码字段的功能它不接受它。 它只是跳过它(意思是它不接受我在密码字段中写的内容)并传递它然后执行该功能。 anyidea为什么? 感谢

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!

1 个答案:

答案 0 :(得分:0)

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

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