将用户名信息保存到服务器而不是NSDefaults(iOS)

时间:2017-02-07 17:01:08

标签: ios swift xcode post user-data

我有一台服务器用于在我的应用程序中存储用户名和密码数据。在测试应用程序时,我使用NSDefaults将所有内容保存到本地设备,但现在应用程序即将完全启动,我试图将它们保存到服务器,因为它对用户而言更安全。信息。

当我把它保存到NSDefaults时,这很简单,也很简短。然而,现在,我正在尝试将数据发布到服务器并继续获取构建错误。为了实现这一目标,我需要改变什么?我不完全了解POST和GET的工作原理吗?谢谢。现在使用Swift 2,不是我的选择,我更喜欢3,但我的老板还没有让我们更新它。

当前错误来自POST USER DATA TO SERVER部分,其中xcode声称userNmeTxt无法转换为NSData。提前谢谢。

编辑:错误在第87行:"无法转换UITextField类型的值!预期参数类型NSData!"

import UIKit

class UserNameViewController: AuthorizationViewController {

@IBOutlet weak var userNameTxt: UITextField!
@IBOutlet weak var continueBtn: UIButton!

var userModel: ProfileModel!

//MARK: - SYSTEMS METHODS

override func viewDidLoad() {
    super.viewDidLoad()
    userNameTxt.delegate = self
    userNameTxt.autocapitalizationType = .Sentences
    setEnabledButton()
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBarHidden = false
    self.navigationItem.leftBarButtonItem  = getBackButton()
    self.title = ""
}

override func viewWillDisappear(animated: Bool) {
    self.navigationController?.navigationBarHidden = true
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    continueBtn.layer.cornerRadius = 10
}

override func popToRoot(sender:UIBarButtonItem){
    self.navigationController!.popViewControllerAnimated(true)
}

//MARK: - CHECK FOR AVALABILITY

func setEnabledButton(){
    if userNameTxt.text == "" {
        continueBtn.backgroundColor = UIColor.lightGrayColor()
    } else {
        continueBtn.backgroundColor = UIColor(colorLiteralRed: 63.0/255.0, green: 220.0/255.0, blue: 236.0/255.0, alpha: 1.0)
    }
    continueBtn.userInteractionEnabled =  userNameTxt.text != ""
}

//MARK: - POST USER DATA TO SERVER

func postData(url: String, params: Dictionary<String, String>, completionHandler: (data: NSData?, response: NSURLResponse?, error: NSError?) -> ()) {

    // Indicate download
    UIApplication.sharedApplication().networkActivityIndicatorVisible = true

    let url = NSURL(string: "myPlaceholderURLgoesHere")!
    //        print("URL: \(url)")
    let request = NSMutableURLRequest(URL: url)
    let session = NSURLSession.sharedSession()
    request.HTTPMethod = "POST"
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    // Verify downloading data is allowed
    do {
        request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(params, options: [])
    } catch let error as NSError {
        print("Error in request post: \(error)")
        request.HTTPBody = nil
    } catch {
        print("Catch all error: \(error)")
    }

    // Post the data
    let task = session.dataTaskWithRequest(request) { data, response, error in
        completionHandler(data: userNameTxt, response: userModel, error: error)

        // Stop download indication
        UIApplication.sharedApplication().networkActivityIndicatorVisible = false
        // Stop download indication

    }

    task.resume()

}

//MARK: - SEGUE

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if segue.identifier == "toPassword"{
        let controller = segue.destinationViewController as! PasswordViewController
        controller.userModel = userModel
    }
}

//MARK: - IB ACTIONS
@IBAction func continuePressed(sender: AnyObject) {
    userModel.userNickName = userNameTxt.text!
    performSegueWithIdentifier("toPassword", sender: self)
}
}

extension UserNameViewController: UITextFieldDelegate{
func textFieldDidEndEditing(textField: UITextField) {
    self.setEnabledButton()
}
}

3 个答案:

答案 0 :(得分:0)

您需要更改一些事项。

userNameTxt不是用户名,而是包含用户名的UITextField。您需要的文字是userNameTxt.text?

如果该功能需要数据,则必须先将文本转换为数据

let task = session.dataTaskWithRequest(request) { data, response, error in
    completionHandler(data: userNameTxt.text?.data(using: .utf8), response: userModel, error: error)

答案 1 :(得分:0)

我假设您必须将数据发送到服务器。

如果您还没有,可以将数据保存在钥匙串访问中,请参阅:SO: Keychain Access

为了解决错误,请使用错误消息和代码行编辑问题(如果可能)。

我建议您使用Alamofire进行POST / GET(REST)。要使用Alamofire,您需要有关Cocoapods的基本知识。从长远来看,它会更好。

注意:发出请求时可能会出现两种错误结果。

1)数据格式不正确或来自您身边的错误

2)服务器端的后端错误导致服务器错误。

可以使用POST从您的设备发送数据,其中数据位于请求的BODY或HEADER中。通常它在体内(alamofire方法中的参数)。

以下是一个例子:

import Alamofire 
...

// MARK:- Login Feature - Universal Met for login
internal static func loginWith(serverUrl: String, parameters: [String: AnyObject]?, headers: [String: String]?, notificationName: String, serviceType: LoginService)
{
    Alamofire.request(.POST, serverUrl, parameters: parameters, headers: headers).responseJSON
    { (response) in
        print("\n Login feature - \n")
        print(" Login url - \(serverUrl)\n")
        print(" Login parameters - \(parameters)\n")
        print(" Login notificationName - \(notificationName)\n")
        print(" Login response - \(response)\n")
        EXCDataParserH.parseResponseFrom(ServiceType.Login(type: serviceType),
            operation: nil,
            data: response.data,
            notification: notificationName)
    }
}

答案 2 :(得分:0)

不是在每次发出服务器请求时写下整个内容,而是尝试执行以下操作:

import Foundation
import SystemConfiguration

class HTTPHelper{
class func httpPostDataDic(postURL:NSURL,postString:NSString,completionHandler:@escaping (NSDictionary?, NSError?) -> Void ) -> URLSessionTask{
    var responseResultData: NSDictionary = NSDictionary()
    let request = NSMutableURLRequest(url:postURL as URL);
    request.httpMethod = "POST";// Compose a query string
    request.httpBody = postString.data(using: String.Encoding.utf8.rawValue);
    print(request)

    let task = URLSession.shared.dataTask(with: request as URLRequest) {
        data, response, error in
        if error != nil
        {
            print("error=\(error)")
            completionHandler(nil, error as NSError?)
            return
        }
        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        if let responseString = responseString {
            print("responseString = \(responseString)")
        }
        do {
            let myJSON =  try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
            responseResultData=myJSON!
            completionHandler(responseResultData, nil)
        } catch {
            print(error)
        }
    }
    task.resume()
    return task


}
}

现在,只要您需要发出服务器POST请求,请在ViewController类中执行以下操作:

//Requesting server
func requestServer() -> Void{
    let postvariable = "Value"

    let url = URL(string: "your url")!
    let postString = "postkey=\(postvariable)"
    HTTPHelper.httpPostDataDic(postURL: url as NSURL, postString: postString) {
        (responseResult, error) -> Void in
        DispatchQueue.main.async {
            if error != nil{
                print(error ?? "unknown")
            }
            else{
                print(responseResult ?? "unknown result")
//Parse your response
                self.parseResult(result: responseResult!);
            }
        }
    }


}

我可以问你一件我在你的问题中无法理解的事情。 您如何在服务器中保存登录凭据?我的意思是,如果您在服务器中保存登录凭据,您将如何验证用户对这些已保存凭据的访问权限?