Skip to main content

Posts

Showing posts with the label Rest API

Rest Api in swift 4 Using URLSession And JSONSerialization Xcode 9 Example

A Short Example call Rest Api with JSON Foundation framework NSURLSession and NSJSONSerialization -



Getting Started - Basic Rest API request with Swift 4

func restApiRequest(){
let sessionConfig = URLSessionConfiguration.default
// Session Configuration
        let sessionUrl = URLSession(configuration: sessionConfig)
// Load configuration into Session
        let url = URL(string: "YOUR URL STRING")!
        let task = sessionUrl.dataTask(with: url, completionHandler: {
            (data, response, error) in
                if error != nil {
                    print(error!.localizedDescription)
                } else {
                    do {
                        if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any]
                        {
                            print(json)
                        }
                    } catch {
                        print("error in JSONSerialization")
               …