我使用 AFNetworking
在我的 iOS
应用程序上设置了一个本地服务器,当我使用我的 simulator
时它工作正常但是当我插入我的 iPhone 我得到一个错误:无法连接到服务器
func postStripeToken(token: STPToken) {
let URL = "http://localhost/donate/payment.php"
let params : [String: AnyObject] = ["stripeToken": token.tokenId,
"amount": totalPrice,
"currency": "usd",
"description": self.emailTextField.text!]
let manager = AFHTTPRequestOperationManager()
manager.securityPolicy.allowInvalidCertificates = true;
manager.POST(URL, parameters: params, success: { (operation, responseObject) -> Void in
if let response = responseObject as? [String: String] {
UIAlertView(title: response["status"],
message: response["message"],
delegate: nil,
cancelButtonTitle: "OK").show()
这是我得到错误的代码。我尝试将 localhost
更改为我的计算机 IP
地址,但这并没有解决我的问题。谢谢!
最佳答案
当 url 包含 'localhost' ("http://localhost/donate/payment.php"
) 时,它表示当前设备,因此您正试图在您的 iphone 上访问您的服务器,而不是在网络上。在 url 中使用你的服务器 ip 而不是 localhost(确保你也在同一个网络上)你可能需要将你的服务器端口号添加到它使用的 url,例如“http://192.168.0.10:8080/donate/payment.php”
关于ios - iPhone 设备不连接到应用程序中的本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38732567/