matt standley
matt standley

Reputation: 11

iPhone devices don't connect to Local server in App

I set up a a local server on my iOS app using AFNetworking and it works fine when I'm using my simulator but when I plug in my iPhone I get an error: cannot connect to server

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()

This is the code where I get the error. I tried changing localhost to my computers IP address but that did not resolve my problem. Thanks!

Upvotes: 0

Views: 3719

Answers (1)

Fonix
Fonix

Reputation: 11597

When a url contains 'localhost' ("http://localhost/donate/payment.php") it means the current device, therefore you are trying to access your server on your iphone, instead of on the network. Use your servers ip instead of localhost in the url (make sure you are on the same network as well) you may need to add your servers port number to the url that it uses eg "http://192.168.0.10:8080/donate/payment.php"

Upvotes: 4

Related Questions