0
votes

My friend coded server and I tried to connect my app to the server. but now I'm in problem with connecting the server. After searching, I found lots of things. (how to get access token, use SHA256, Alamofire library,,,etc)

But it is difficult to see our API and code the swift app. I hope somebody helps good example for start to connect server with my app.

below is our API for signup.

// ch-----.co.kr:8081/api/user/createAccount

Request Type: POST, json/urlencoded

Parameter

userId : user account name
userPassword : user password for account
userEmail : user email for account

when success,

{ "meta": "createAccount", "data": "success" }

when fail,

{ "meta": "createAccount", "data": "failed" }

hope to use alamofire and get the first step example. please..

and code is in part of my register page.

//MARK:- Infoamation 
@IBOutlet weak var username: UITextField!
@IBOutlet weak var email: UITextField!
@IBOutlet weak var password: UITextField!
@IBOutlet weak var passwordRe: UITextField!

@IBOutlet weak var admitUseRule: UISwitch!
@IBOutlet weak var admitPrivacyRule: UISwitch!

//register action
@IBAction func registerAction(sender: AnyObject) {

    let usernameText = username.text
    let emailText = email.text
    let passwordText = password.text
    let passwordReText = passwordRe.text


    //check if filling out ID
    guard usernameText?.isEmpty != true else{
        displayAlert("아이디를 입력하세요")
        return
    }

    //check if the id counts less than 20
    guard usernameText?.characters.count < 20 else{
        displayAlert("아이디를 20자 이내로 입력하세요")
        return
    }

    //check iD validation
    guard isValidID(self.username.text!) != false else{
        displayAlert("아이디를 영어와 숫자의 조합으로 입력하세요")
        return
    }

    //check if filling out email
    guard emailText?.isEmpty != true else{
        displayAlert("이메일 입력하세요")
        return
    }

    //check email validation
    let validLogin = isValidEmail(self.email.text!)

    guard validLogin != false else {
        displayAlert("정확한 이메일을 입력하세요")
        return
    }

    //check if the user fill out password
    guard passwordText?.isEmpty != true else {
        displayAlert("비밀번호를 입력하세요")
        return
    }

    //check the password is same
    guard passwordText == passwordReText else {
        displayAlert("입력한 비밀번호가 같지 않습니다")
        return
    }

    //admit for useRule
    guard admitUseRule.on == true else {
        displayAlert("이용약관에 동의해주세요")
        return
    }

    //admit for privacyRule
    guard admitPrivacyRule.on == true else {
        displayAlert("개인정보 취급 방침에 동의해주세요")
        return
    }

    //connect server
    ////////////////////



    dismissViewControllerAnimated(true, completion: nil)


}
1

1 Answers

0
votes

I would suggest to do following:-

     enum UserValidity: String {
            case Valid = "success"
            case Invalid = "failed"
        }
    // Note: if you wish to define these as Optionals i.e. e.g. var userEmail:String?, userPasswod:String? then you need to unwrap them before when making POST request
   // var userEmail:String = "" , userPassword :String = ""

        // Your Parameters since you already have variables consisting of email id and password use those in code
        // let parameters =  ["uEmail": userEmail,"uPass":userPassword]

    // if defined as Optionals change above to
     let parameters = ["uEmail" : "\(emailText)", "uPass": "\(passwordText)"]

        //Post your parameters to PHP file and since response expected as mentioned is { "meta": "createAccount", "data": "success" } which is json Format 
                    Alamofire.request(.POST,"http://co.kr:8081/api/user/createAccount", parameters: parameters)
                        .validate().responseJSON { response in
                            if let responseJson = response.result.value {
                                let validity = responseJson["data"] as! String
                                if validity == UserValidity.Valid.rawValue {
                                 // user is valid 
                                }
                             else {
                           // user is invalid
                                }

                            }
                    }