Simple, strictly focused micro-library you should use as foundation for your web service client code. Or
This is really simple micro-library. It could have just few files but I deliberately split into multiple files, for clarity and easier maintenance.
I expect that you may need to tweak some things here and there. Add your own custom
NetworkError case or similar. That’s why…
No CocoaPods. No Carthage.
Not everything needs to be packaged like external library. It’s ok to just copy stuff into your project.
(1) I usually create
Vendor folder group for all 3rd-party code, then inside it I have
Avenue folder and copy the files from
Vendor/ Avenue/ Atomic.swift AsyncOperation.swift Network-Extensions.swift NetworkError.swift NetworkHTTPMethod.swift NetworkOperation.swift NetworkPayload.swift NetworkSession.swift NetworkTask.swift ServerTrustPolicy.swift
Network-Extensions.swift, read the ATTENTION notice and act accordingly, if needed.
NetworkSessionis main class. To write your API wrapper, you should subclass it and simply provide the desired
IvkoServicein the Demo project, as examples. Write as many of these as you need.
Wrap each network request you make into
NetworkOperationand use an
OperationQueueto manage them. There should be no need to subclass it further but don’t be afraid to do so.
NetworkErrorenum with more cases if you need them.
NetworkPayloadis particularly useful struct since it aggregates
URLRequest+ response headers, data and error and gives you simple speed metering capability by recording start and end of each network call.
ServerTrustPolicyis directly picked up from Alamofire; it’s great as it is and there’s no need to reinvent the wheel.
AsyncOperation is my own simple subclass which makes sure that
Operation is marked
finished only when the network async callback returns.
Atomic.swift is required by AsyncOperation.
I have extended URLSessionTask with additional properties.
Such trickery is required to overcome URLSession/URLSessionTask API design, which is not compatible with
Operations. Read the original post on my blog for more thoughts on this.
Apple insists that you should re-use
URLSession instance across your app or at least have one per host.
Thus I needed to open some doors to re-route data received in the
NetworkSession in Avenue) into the
URLSessionDataTask created internally inside the
I have used it and tested it extensively (on live projects) in iOS 10+ and watchOS 3.2+ and tvOS 10+. It probably works in earlier versions but I have not tried it. I suspect it will work just fine on macOS too but I have not tried that either.
The latest version is Swift 4.2 compatible.
MIT License, like all my open source code.
Alamofire community for their invaluable work over the years. I don’t use the library itself, but there are re-usable gems in it (like ServerTrustPolicy handling).
I want re-iterate what Marcus said at the end of his talk:
Write it [network code] yourself. I guarantee code you write yourself will be faster than any generic code, that is the law. Whenever you write something that is very specific, it is going to be faster than generics.
Bad SSL in many ways, fantastic resource to test your code.
nscurl --help(in your macOS Terminal)