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'
Ruby

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)")
}
Swift

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)"
  }
}
Swift

Catch will reject with a NetworkError:

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

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")
}
Swift

GitHub

Latest commit to the undefined branch on unknown
Download as zip