Giter Site home page Giter Site logo

chandlerdea / networkingcontroller Goto Github PK

View Code? Open in Web Editor NEW
3.0 2.0 1.0 72 KB

A simple, thread safe networking library for iOS

License: MIT License

Swift 97.27% Objective-C 1.10% Ruby 1.63%
swift-4 nsurlsession nsurlrequest nsurlprotocol ios macos watchos tvos cocoa-touch cocoa

networkingcontroller's Introduction

NetworkingController

This is a simple, lightweight library for making asynchronous network requests on iOS. It is thread safe, meaning that networking requests can be made from any thread, without the dangers of the common pitfalls found in multi threaded environments.

Requirements

This is written in Swift 4, works with macOS, iOS, watchOS, and tvOS.

Installation

You can install this library with Carthage, Cocoapods, or Swift's package manager

Carthage

Add this line to you Cartfile:

github "Chandlerdea/NetworkingController"

Run carthage update and then add the created framework in $(SRCROOT)/Carthage/build/iOS to the Embedded Binaries section of you project.

Cocopods

I'm having trouble linting the cocoapod, so cocoapods support isn't available just yet. I'll be adding it as soon as I figure out the issue.

Swift Package Manager

In your Packages.swift file, add this code

import PackageDescription

let package = Package(
    url: "https://github.com/Chandlerdea/NetworkingController/NetworkingController.swift"
    majorVersion: 1
)

How to Use

Using the library is pretty simple. The NetworkingController is given a URLRequest to perform, and gives its delegates the data returned from the request. There is only one public function, perform(request:), and successDelegate and errorDelegate properties. A typical use looks like this:

class Foo {

  private lazy var controller: NetworkingController = {
    let controller: NetworkingController = NetworkingController()
    controller.successDelegate = self
    controller.errorDelegate = self
    return controller
  }()

  func doStuff() {
    let url: URL = URL(string: "http://www.google.com")!
    let request: URLRequest = URLRequest(url: url)
    self.controller.perform(request: request)
  }
}

extension Foo: NetworkingControllerErrorDelegate {

  func requestDidFail(_ request: URLRequest, error: NSError, status: URLResponseStatus?) {

  }

  func sessionDidFail(_ error: NSError?) {

  }
}

extension Foo: NetworkingControllerSuccessDelegate {

  func requestDidComplete(_ request: URLRequest, data: Data) {

  }
}

Authentication

This library handles certificate pinning out of the box. All you need to do is provide the certificates, and the library will handle the authentication challenge correctly. For HTTP basic authentication, there is a function on NetworkingControllerAuthenticationDelegate:

func requestDidReceiveAuthenticationChallenge(_ request: URLRequest) -> (username: String, password: String)?

If you implement this method, the library will handle the authentication challenge correctly. The library stores the URLCredential perminently, so this will only need to be done once per app install. If you are using HTTPS, but the endpoint your hitting doesn't require authentication, you can specify to continue with the request, which causes the URLSession to perform the request. You just need to implement this method in NetworkingControllerAuthenticationDelegate:

    func shouldProceedWithAuthenticationChallendWithoutCredentials(_ request: URLRequest) -> Bool

If you have any critiques, or ideas, open a PR or let me know!

networkingcontroller's People

Contributors

chandlerdea avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

surgiollc

networkingcontroller's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.