ios - iPhone 设备不连接到应用程序中的本地服务器

标签 ios mysql swift2 localhost afnetworking

我使用 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/

相关文章:

ios - 使用 UIWebView 而不是 native UI 元素来显示复杂的响应式布局是一个好习惯吗?

iphone - 带有自定义子对象的 NSCoding

Mysql存储过程+更新查询

php - while循环问题

swift - 将我的代码变成更好的类似 Swift 的 FizzBu​​zz 响应答案

iphone - 计算给定宽度和字体大小的行数

objective-c - 如何实现在一个部分中点击一个 UITableViewCell 导航到多个 UITableViewCells 以获取详细信息?

php - 将 RegEx 从 MySQL 提取到 PHP 中

ios - 在 Swift Alamofire 中写入的 JSON 类型无效

ios - XCode7、Swift 2、使用解析数据填充 TableView