CODEX- 5 JSON READ USING ALAMOFIRE

 

Alamofire

    • Install Cocoapods using below given code :
      • Open Terminal and write below code.
      • $ gem install cocoapods.
      • To integrate Alamofire into your Project Create Podfile into your project.
      • Your file name should with case sensitive as ‘Podfile’.
      • Select empty file into your project and paste below code show :
        source 'https://github.com/CocoaPods/Specs.git'
        platform :ios, '10.0'
        use_frameworks!
        
        target '<Your Target Name>' do
            pod 'Alamofire', '~> 4.0'
        end
      • Now,
      • go to project folder in terminal and install pod by write code as :
      • $ pod install
      • Now, you integrated Alamofire into your project.
      • HTTPS Methods in Alamofire :
        • GET
        • POST
        • HEAD
        • PUT
        • PATCH
        • OPTION
        • DELETE
        • TRACE
        • CONNECT
  • Alamofire GET requestNow import Alamofire using

    Under viewDidLoad() type the following code

 

Uploading Data,file and multipart to the server

let imageData = UIPNGRepresentation(image)!

Alamofire.upload(imageData, to: "YOUR URL").responseJSON { response in
    debugPrint(response)
}

Uploading a File

let fileURL = Bundle.main.url(forResource: "video", withExtension: "mov")

Alamofire.upload(fileURL, to: "YOUR URL").responseJSON { response in
    debugPrint(response)
}

Uploading Multipart Form Data

Alamofire.upload(
    multipartFormData: { multipartFormData in
        multipartFormData.append(unicornImageURL, withName: "unicorn")
        multipartFormData.append(rainbowImageURL, withName: "rainbow")
    },
    to: "YOUR URL",
    encodingCompletion: { encodingResult in
        switch encodingResult {
        case .success(let upload, _, _):
            upload.responseJSON { response in
                debugPrint(response)
            }
        case .failure(let encodingError):
            print(encodingError)
        }
    }
)

Leave a Reply

Your email address will not be published. Required fields are marked *