NetworkUtils

NetworkUtils is a package for implementing HTTP network requests in Swift for iOS. The goal of the project is to replicate the functionality of the axios npm package used in nodejs.

It is built off of the Foundation URL Loading System (similar to Alamofire). NetworkUtils uses Ryu Games's SwiftPromises library for promise support.

Installation

NetworkUtils is available through CocoaPods. To install
it, simply add the following lines to your Podfile:

pod 'NetworkUtils'

And run pod install.

Example Usage

HTTP Requests

Making an HTTP request with NetworkUtils is really simple. Use the NetworkUtils.main singleton object and one of the HTTP methods: post, get, put and delete.

Here is an example HTTP GET request:

let networkUtils = NetworkUtils.main

networkUtils.get("http://ip-api.com/json").then {(data) in
  print("Data found: \(data)")
}.catch {(error) in
  print("Error: \(error.localizedDescription)")
}

Error Handling

NetworkUtils offers a very basic subclass of Error named NetworkError:

public struct NetworkError: Error {
  public let msg: String
  public let code: Int
  public var localizedDescription: String {
    return "There was a Network Error with code \(code) and a message: \(msg)"
  }
}

Catch will reject with a NetworkError:

}.catch {(error) in
  let code = error.code
  let msg = error.msg
  let localizedDescription = error.localizedDescription
}

Reachability

NetworkUtils also offers reachability services. Access reachability with NetworkUtils.reachability such as in the following example:

let reachability = NetworkUtils.reachability

switch reachability.connection {
case .wifi:
    print("Reachable via WiFi")
case .cellular:
    print("Reachable via Cellular")
case .none:
    print("Not Reachable")
}

GitHub