Latest 0.3.1
Homepage https://github.com/Ryucoin/NetworkUtils
License MIT
Platforms ios 12.0
Dependencies PromisesSwift
Authors

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.

Latest podspec

{
    "name": "NetworkUtils",
    "version": "0.3.1",
    "summary": "Swift package for handling HTTP requests",
    "homepage": "https://github.com/Ryucoin/NetworkUtils",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "WyattMufson": "[email protected]"
    },
    "source": {
        "git": "https://github.com/Ryucoin/NetworkUtils.git",
        "tag": "0.3.1"
    },
    "social_media_url": "https://twitter.com/_ryugames",
    "platforms": {
        "ios": "12.0"
    },
    "swift_version": "5",
    "source_files": "NetworkUtils/Classes/**/*",
    "dependencies": {
        "PromisesSwift": []
    }
}

Pin It on Pinterest

Share This