无法通过 iPhone 将 php 文件与 Xcode 连接 [英] Can't connect php file with Xcode through iPhone

查看:24
本文介绍了无法通过 iPhone 将 php 文件与 Xcode 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个包含数据库的应用程序.我使用 PHP 连接到 MySQl 数据库.这是我的连接代码:

I am working on an app which includes a database. I use PHP to connect to a MySQl database. Here is my connection code:

var request = URLRequest(url: URL(string: "http://localhost/Workspace/Twitter/register.php")!)
request.httpMethod = "POST"
let body = "Username=\(UsernameTXF.text!.lowercased())&password=\(PassTXT.text!.lowercased())"
request.httpBody = body.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) { data, response, error in guard let data = data, error == nil else {                                                 // check for fundamental networking error
     print("error=\(error)")
     return
}

这段代码在模拟器上运行时完美无缺,但是当我在 iPhone 上运行时,它给了我以下错误:

This code works flawlessly when I run it on simulator,but when I run it on iPhone it gives me following error:

error=Optional(Error Domain=NSURLErrorDomain Code=-1004 "无法连接到服务器." UserInfo={NSUnderlyingError=0x17425dbb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://localhost/Workspace/Twitter/register.php, NSErrorFailingURLKey=http://localhost/Workspace/Twitter/register.php, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=无法连接到服务器.})

error=Optional(Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x17425dbb0 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={_kCFStreamErrorCodeKey=61, _kCFStreamErrorDomainKey=1}}, NSErrorFailingURLStringKey=http://localhost/Workspace/Twitter/register.php, NSErrorFailingURLKey=http://localhost/Workspace/Twitter/register.php, _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61, NSLocalizedDescription=Could not connect to the server.})

推荐答案

localhost"在你的 iPhone 上与在你的桌面上运行模拟器的localhost"完全不同.如果您想在 iPhone 上运行此代码,请指定一个可以从手机访问的域.即,使用数字 IP 地址或指定运行您的 Web 服务的域.

"localhost" on your iPhone is an entirely different thing than "localhost" on your desktop running a simulator. If you want to run this code on your iPhone, specify a domain that can be reached from the phone. I.e., use a numeric IP address or specify a domain where your web service is running.

这篇关于无法通过 iPhone 将 php 文件与 Xcode 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆