Connect to PHP Server Using Swift

Connect to PHP Server using Swift

I'm not familiar with your third party library, but you should be able to achieve the same with the built in methods, since it seems to be a simple synchronous request. Try this:

let url = NSURL(string:"http://localhost/project")
let request = NSURLRequest(URL:url)
var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)

let results = NSString(data:reply, encoding:NSUTF8StringEncoding)

// Continue your processing of results here...

Update
To use POST, employ an NSMutableURLRequest. General form is something like the following. The difficult to find part is NSURLProtocol.setProperty(...). See this page.

let url = NSURL(string:"http://localhost/project")
let cachePolicy = NSURLRequestCachePolicy.ReloadIgnoringLocalCacheData
var request = NSMutableURLRequest(URL: url, cachePolicy: cachePolicy, timeoutInterval: 2.0)
request.HTTPMethod = "POST"

// set Content-Type in HTTP header
let boundaryConstant = "----------V2ymHFg03esomerandomstuffhbqgZCaKO6jy";
let contentType = "multipart/form-data; boundary=" + boundaryConstant
NSURLProtocol.setProperty(contentType, forKey: "Content-Type", inRequest: request)

// set data
var dataString = "your data here" + boundaryConstant
let requestBodyData = (dataString as NSString).dataUsingEncoding(NSUTF8StringEncoding)
request.HTTPBody = requestBodyData

// set content length
NSURLProtocol.setProperty(requestBodyData.length, forKey: "Content-Length", inRequest: request)

var response: NSURLResponse? = nil
var error: NSError? = nil
let reply = NSURLConnection.sendSynchronousRequest(request, returningResponse:&response, error:&error)

let results = NSString(data:reply, encoding:NSUTF8StringEncoding)

Connecting Swift with php server

dataTaskWithRequest is an asynchronous operation. Printing the response code immediately after the task starts will obviously print nil, as it hasn't had time to get a value. The completion handler already has a response object you can use, so inside your completion handler, call print(response) to see the properties and try to get a response code.

How to get swift to work with an existing php/mysql hashed passwords

You will still have to do the password verification on the server using PHP by creating an API that the iOS app can talk to.

What you have right now is a Server which holds the website code and the database. When a user logs in via a web browser (the client) the web browser sends the username/password to the server and the server does the work of verification and sends the response to the client.

An iOS app will also be a client. When a user enters a username/password on the iOS client, take that information and send it to the server and have the server do the verification and send back a response to the iOS client.

Do a search for REST api to see how to build what you are looking for on the server. And look at NSURLSession in the apple docs to see how to communicate with your server.

How to make HTTP request to php server in Swift iOS

add request.HTTPMethod = "POST" since you are trying to do a post request, aren't you?

and btw: when i try to use your URL outside of xcode, the request works (status 200). the problem seems to be in your php script:

Notice: Undefined index: userid in /home/techicom/public_html/varun/ios-api/userRegister.php on line 4

Notice: Undefined index: password in /home/techicom/public_html/varun/ios-api/userRegister.php on line 5
{"status":"error","message":"Missing required field"}

Swift 3 upload image to PHP-Server

Change the "1" in your declare for imgData to 0.1

let imgData = UIImageJPEGRepresentation(image, 0.1)!

That changes the quality of the image uploaded. Where 1 being the highest quality and 0 being the lowest (most compressed). I had the same issue when I used "1" but worked for me when I set to a lower quality. The image quality was noticeably bad for me so this solution may work for you too. You can also try another quality/compression amount, such as 0.2 or 0.5 or higher. That may also work for you, you just have to test it.

I speculate the reason for this is that the image was too big (when not compressed) for the server to handle it.



Related Topics



Leave a reply



Submit