iOS: get response from PHP -
i post , data to/from mysql database php. want response string.
for example:
a user wants login in app. before storing data , proceed i'll have check if username/password pair exists in database.
@ibaction func login(sender: anyobject) { let request = nsmutableurlrequest(url: nsurl(string: "http://example.com/checklogin.php")!) request.httpmethod = "post" let poststring = "username=\(username.text!)&password=\(password.text!)" request.httpbody = poststring.datausingencoding(nsutf8stringencoding) }
this post username , password checklogin.php file:
<?php $root = realpath($_server["document_root"]); include "$root/config.php"; $username = $_post['username']; $password = $_post['password']; $statement = $pdo->prepare("select * contact username = :username , password = :password"); $statement->execute(array('username' => $username, 'password' => $password)); $row = $statement->rowcount(); if ($row == 1) { /* send "true" xcode! */ } else { /* send "false" xcode! */ }
this check if username/password pair exists.
i want "true" or "false" response in ios. if it's true user gets signed in. if it's "false" user gets alert check login data.
what common way send/get , handle responses this? common way achieve this?
you can not return data app directly there several ways send data app : simple way :
use on php echo true or false ,
if ($row == 1) { echo "true"; } else { echo "false"; }
add response in swift app , check if it's true or false if statement:
let task = nsurlsession.sharedsession().datataskwithrequest(request) { data, response, error in guard error == nil && data != nil else { // check fundamental networking error print("error=\(error)") return } if let httpstatus = response as? nshttpurlresponse httpstatus.statuscode != 200 { // check http errors print("statuscode should 200, \(httpstatus.statuscode)") print("response = \(response)") } let responsestring = nsstring(data: data!, encoding: nsutf8stringencoding) print("responsestring = \(responsestring)") } task.resume()
note: aware it's not way simple way said it's example...
if want more data server use json
Comments
Post a Comment