错误域= NSURLErrorDomain代码= -1004

时间:2017-06-27 09:11:40

标签: swift xcode

我是初学者,在手机上部署我的应用时遇到了问题。应用程序在模拟器上工作正常,但当我在手机上运行并尝试在手机上发送注册表时,我收到了以下错误

  

FAILURE:Error Domain = NSURLErrorDomain Code = -1004"无法连接到服务器。" UserInfo = {NSUnderlyingError = 0x174244da0 {错误域= kCFErrorDomainCFNetwork代码= -1004"(null)" UserInfo = {_ kCFStreamErrorCodeKey = 61,_kCFStreamErrorDomainKey = 1}},NSErrorFailingURLStringKey = http://localhost/Syafiqah_153046u/register.php,NSErrorFailingURLKey = http://localhost/Syafiqah_153046u/register.php,_ kCFStreamErrorDomainKey = 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.
    }
   }

非常感谢您的帮助。谢谢你的时间

1 个答案:

答案 0 :(得分:1)

从iPhone访问localhost只会进行环回/尝试连接到自身(如果它支持?)。

您需要做的是找到桌面计算机的IP(例如,如果是Windows,请转到命令提示符并键入ipconfig或转到网络和共享中心并查找连接状态。