0

I'm new to the iOS world, but I have been working as a Software Developer and worked on many Web Applications with Laravel and PHP.

My Question is now if PHP is a good server-side language for Swift, then what is the common way to communicate with a Server in Swift.

I'm thinking to make a REST API call to a Laravel or PHP Application to get and store all Datas I need for my Project.

Thanks for your Tips

AppHero2
  • 681
  • 7
  • 25
mrfr34k
  • 85
  • 11
  • REST API call is good approach. – PiyushRathi Nov 28 '17 at 07:47
  • First of all: using a REST API is indeed a great idea, Laravel already returns JSON by default and Swift can parse this very easily back in dictonaries (for example). However the way you ask it is heavily opinion based and because of that offtopic on this site. Best of luck. – online Thomas Nov 28 '17 at 07:55
  • It is good one. Even has special libs as Passport for api auth: https://laravel.com/docs/5.5/passport – Adam Kozlowski Nov 28 '17 at 08:27
  • I did make answer for your question, @mrfr34k. I wish you check right answer if you got any helpful. – AppHero2 Nov 28 '17 at 10:33

1 Answers1

2

Here is your way for beginners (Swift, iOS developer)

Using cocoa framework in Swift

URLSession

Initialize a URL object and a URLSessionDataTask from URLSession. Then run the task with resume().

let url = URL(string: "http://www.stackoverflow.com")

let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in
    print(NSString(data: data!, encoding: String.Encoding.utf8))
}

task.resume()

NSURLConnection

First, initialize a URL and a URLRequest:

let url = URL(string: "http://www.stackoverflow.com")
let request = URLRequest(URL: url!)

Then, you can load the request asynchronously with:

NSURLConnection.sendAsynchronousRequest(request, queue: 
NSOperationQueue.mainQueue()) {(response, data, error) in
    print(NSString(data: data!, encoding: NSUTF8StringEncoding))
}

Using Alamofire - Awesome framework

https://github.com/Alamofire/Alamofire

Making a Request

import Alamofire

Alamofire.request(.GET, "http://httpbin.org/get")

Response Handling

Alamofire.request(.GET, "http://httpbin.org/get", parameters: ["foo": "bar"])
         .response { request, response, data, error in
              print(request)
              print(response)
              print(error)
          }
AppHero2
  • 681
  • 7
  • 25