【问题标题】:Read return value php on IOS application在 IOS 应用程序上读取返回值 php
【发布时间】:2017-04-02 12:17:27
【问题描述】:

您好,我正在编写一个 ios swift 3 应用程序来与网站通信,该应用程序在做了很多事情后应该返回一个 false 或 true 的类型值,但它不会发生你可以告诉我我在哪里错误以及如何纠正错误!

快速返回值: ....response = 可选({ URL:“http://....myurl.php”}.....

SWIFT 代码

let myUrl = URL(string: "http://....myurl.php");

var request = URLRequest(url:myUrl!)

request.httpMethod = "POST"// Compose a query string

let postString = "username=James&password=Bond";

request.httpBody = postString.data(using: String.Encoding.utf8);

let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in

    if error != nil
    {
        print("error=\(error)");
       // return false
    }

    print("response = \(response)")

}
task.resume()

return 0;

PHP 代码:

include 'user.php';

$user = new User();
$username= $_REQUEST["username"];
$password = $_REQUEST["password"];

 if($user->login($username,$password)==true){
        echo json_encode("true"); 
 }

 else{
     echo json_encode("false"); 
 }

错误图片:

【问题讨论】:

    标签: php ios json swift


    【解决方案1】:

    您需要查看数据而不是响应。 也许您应该将返回值封装在 PHP 代码中,例如:

    if($user->login($username,$password)==true){
        echo '{"success":true}';
     }else{
         echo '{"success":false}';
     }
    

    然后快速得到结果:

    func login(request_completed:@escaping (_ succeded:Bool) -> ()) {
        let myUrl=URL(string: "http://....myurl.php");
        var request=URLRequest(url:myUrl!)
        request.httpMethod="POST"
        let postString = "username=James&password=Bond";
        request.httpBody = postString.data(using: String.Encoding.utf8);
    
        let task=URLSession.shared.dataTask(with: request, completionHandler: { data, response, error in
            guard data != nil else {
                print("no data found")
                request_completed(false)
                return
            }
    
            do{
                if let jsonData=try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as? NSDictionary{
                    print(jsonData)
                    let success=jsonData.value(forKey: "success") as! Bool
                    if success{
                        print("login succeded")
                        request_completed(true)
                        return
                     }else{
                        print("login failed")
                        request_completed(false)
                        return
                    }
                }else{
                    print("could not parse json")
                    request_completed(false)
                }
            }catch{
                print("request failed")
                request_completed(false)
            }
        })
        task.resume()
    }
    

    【讨论】:

    • 谢谢,但我在哪里插入您的 swift 代码? @Marie Dm
    • 当我在 Xcode 上启动您的代码时,出现语法错误! @Marie Dm
    • 我添加了上面错误的图像,我写了问题@Marie Dm
    猜你喜欢
    • 2015-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-24
    • 1970-01-01
    • 2015-08-25
    • 2021-05-08
    • 2015-04-29
    相关资源
    最近更新 更多