我是一个初学者,在我的手机上部署我的应用程序时遇到了问题。应用程序在模拟器上运行良好,但当我在手机上运行并尝试在手机上发送注册表时,出现了以下错误
失败:Error Domain=nsurlerdomain Code=-1004“无法连接到服务器。”UserInfo={NSUnderlyingError=0x174244da0{Error Domain=kcfrrordomaincfnetwork Code=-1004“(null)”UserInfo={kCFStreamErrorCodeKey=61,\ukcfstreamerrorddomainkey=1},NSErrorFailingURLStringKey=http://localhost/Syafiqah_153046u/register.php,NSErrorFailingURLKey=http://localhost/Syafiqah_153046u/register.php,\ukcfstreamerrorddomainkey=1,_kCFStreamErrorCodeKey=61,NSLocalizedDescription=无法连接到服务器。}
下面是我的源代码

class ViewController: UIViewController {
    @IBOutlet weak var username: UITextField!
    @IBOutlet weak var password: UITextField!
    @IBOutlet weak var email: UITextField!
    @IBOutlet weak var fullname: UITextField!
    @IBOutlet weak var phone: UITextField!
    @IBOutlet weak var labelMessage: UILabel!

    @IBAction func Register(_ sender: UIButton)
    {
        let URL_USER_REGISTER = "http://localhost/FYP/register.php"

        let parameters: Parameters=[

            "username":username.text!,

            "password":password.text!,

            "email":email.text!,

            "fullname":fullname.text!,

            "phone":phone.text!

        ]

        //Sending http post request

        Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters).responseJSON

            {
                response in

                //printing response

                print(response)



                //getting the json value from the server

                if let result = response.result.value

                {

                    //converting it as NSDictionary

                    let jsonData = result as! NSDictionary

                    //displaying the message in label

                    self.labelMessage.text = jsonData.value(forKey: "message") as! String?

                }
        }
    }

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
   }

非常感谢你的帮助谢谢你抽出时间

最佳答案

从iPhone访问localhost只需要做一个环回/尝试连接到它自己(如果它支持这个功能?).
您需要做的是查找桌面计算机的IP(例如,如果是Windows,请转到命令提示符并键入ipconfig,或转到网络和共享中心并查找连接状态。

关于swift - 错误域= NSURLErrorDomain代码= -1004,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44776365/

10-13 04:29