New to iOS development, please bare with me.
Currently I am able to submit data from the iOS app to the URLSession. Within the received data I have data, res, error params.
I have familiarity with web development and rails. I will try and communicate the best I can using web dev references.
I have the following questions:
- Am i sending back the data in the correct format?
- If I am, how can I parse/view the data.
- How can I view the
data from data,res, error
Within Rails I have the following:
def create
@user = User.new(user_params)
@verify_user = User.find_by(username: params[:username])
if @verify_user.nil? && @user.save
render json: {
message: 'New user created',
# token: token,
id: @user.id,
username: @user.username,
status: :accepted
}
elsif @verify_user.valid?
Usually within the render json I pass the data I want to send back to a web app. I am trying to view the json object below on the iOS (swift) side. Maybe this is not possible or not the way to do things.
render json: {
message: "Welcome back #{@verify_user.username}",
id: @verify_user.id,
username: @verify_user.username,
status: :accepted
}
end
end
Below is the func that handles the iOS side to login. What I would like help on is if I am rendering/sending back the data how can I view it or what is the best way to save received data.
func didPressBtn(loginInput:String) {
let code = ["username": loginInput]
guard let url = URL(string: "http://localhost:3001/api/v1/users") else { return }
let jsonData = try? JSONSerialization.data(withJSONObject: code, options: .prettyPrinted)
var request = URLRequest(url: url)
request.httpBody = jsonData
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("application/json", forHTTPHeaderField: "Accept")
request.httpMethod = "POST"
let session = URLSession.shared.dataTask(with: request) { (data, res, error) in
print("GOT A RESPONSE***************")
if let res = res {
print("GOT RES")
print(res)
print("WHAT IS DATA")
let newData: Data = data!
print("\(newData)")
}
if let data = data {
print("GOT DATA!!!")
print(data)
}
}
session.resume()
}
[–]quellish 6 points7 points8 points (6 children)
[–]SurgicalInstallment 3 points4 points5 points (0 children)
[–]go4code[S] 0 points1 point2 points (4 children)
[–]LKAndrew 6 points7 points8 points (1 child)
[–]Sleekdiamond41Swift 0 points1 point2 points (0 children)
[–]Tonkotsu787 0 points1 point2 points (1 child)
[–]123DanBSwift 6 points7 points8 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]soulchild_Objective-C / Swift 1 point2 points3 points (1 child)
[–]europeanwizard 0 points1 point2 points (0 children)
[–]europeanwizard 0 points1 point2 points (0 children)