Skip to content
Swift package for handling HTTP requests
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
NetworkUtils.xcodeproj
NetworkUtils.xcworkspace
NetworkUtils Update copyright info Jun 17, 2019
NetworkUtilsExample Update copyright info Jun 17, 2019
NetworkUtils_ExampleUITests
Tests
.codecov.yml
.gitignore
.travis.yml
LICENSE
NetworkUtils.podspec
Podfile
Podfile.lock
README.md

README.md

NetworkUtils

Swift package for handling HTTP requests

Build Status codecov Version License Platform

Overview

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 Google's Promises 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)"
  }
}

You can cast the returned Error to a NetworkError and access both of its custom fields, plus its custom localizedDescription:

}.catch {(err) in
  let error = err as! NetworkError
  let code = error.code
  let msg = error.msg
  let localizedDescription = error.localizedDescription
}

Note that localizedDescription will only change if you cast the generic Error to NetworkError.

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

Author

WyattMufson - cofounder of Ryu Games

License

NetworkUtils is available under the MIT license.

You can’t perform that action at this time.