dongpao5261 2017-04-21 12:56
浏览 79

如何从PHP到Swift UIAlertController的回声

It's almost a day since I'm working on my Login page of my App and I want to show to my app the errors (or whatever from the echo of PHP) to my xCode app. I'll show you my PHP file and my xCode

<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{

 $password = $_POST['password'];
 $email = $_POST['email'];

 if($password == '' || $email == '')
 {
    echo 'Please fill all values.';
 }
 else
 {
    require_once('GBconnect.php');
    $sql = "SELECT * FROM Users WHERE email='$email' AND password='$password' OR mobile_no='$email' AND password='$password'";
    $check = mysqli_fetch_array(mysqli_query($connection,$sql));

        if(isset($check))
        {  
            echo 'Login Success';
        }
        else
        { 
            echo 'Email/Phone or Password is wrong.';
        }
        mysqli_close($connection);
 }
}
else
{
    echo 'error';
}

Here's my Swift file:

@IBAction func signUp(_ sender: Any)
{
    let request = NSMutableURLRequest(url: NSURL(string: "http://34.205.37.201/restapi/GBlogin3.php")! as URL)
    request.httpMethod = "POST"

    let logEmail = "email=\(username.text!)&& password=\(password.text!)"
    let logMobile = "mobile_no=\(username.text!)&& password=\(password.text!)"

    if (username.text?.isEmpty)! || (password.text?.isEmpty)!
    {
        //display message
        LoginInfoMyAlertMessage(userMessage: "Please input your Email or Phone and Password.");
    }
    if let checkNum = Int(username.text!)
    {
        print(checkNum)
        request.httpBody = logMobile.data(using: String.Encoding.utf8)
    }

    let task = URLSession.shared.dataTask(with: request as URLRequest)
    {
        data, response, error in

        if error != nil
        {
            print("error=\(String(describing: error))")
        }

        print("response = \(String(describing: response))") 
        let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)
        print("responseString = \(String(describing: responseString))")
    }
    task.resume()

    username.text = ""
    password.text = ""
    return
  • 写回答

2条回答 默认 最新

  • doushi8599 2017-04-21 14:57
    关注

    A couple of things I see:

    1. You specify logEmail but never use it anywhere
    2. You have a space in logMobile but you should not when using application/x-www-form-urlencoded POST data
    3. In a related item, you should use a more robust form encoding than concatenating strings.
    4. You should use HTTP status codes to indicate success or failure, not strings. Use HTTP 200 for success, HTTP 401 for needing credentials, and HTTP 403 for invalid credentials

    With all of that said, you haven't specified what you are seeing when you run the code. If you can do that, we can offer more specific advice. Use POSTMAN to verify that your server side works correctly, then you can ensure your client is working with the server.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度