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
}
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 "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.})