Simple, strictly focused micro-library you should use as foundation for your web service client code.
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.
I usually create
Vendor folder group for all 3rd-party code, then inside it I have
Network folder and add these files:
Vendor/ Network/ 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.
NetworkSession is main class. To write your API wrapper, you should subclass it and simply provide the desired
URLSessionConfiguration you need.
IvkoServicein the Demo project, as examples. Write as many of these as you need.
Wrap each network request you make into
NetworkOperation and use an
OperationQueue to manage them. There should be no need to subclass it further but don’t be afraid to do so.
NetworkError enum with more cases if you need them.
NetworkPayload is particularly useful struct since it aggregates
URLRequest + response headers, data and error and gives you simple speed metering capability.
ServerTrustPolicy is 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.
This trickery is required to overcome URLSession/URLSessionTask API design, which is not compatible with
Operations. Read the related 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.
Ok. But then I need to open some doors to re-route data received in the
NetworkSession) into the
URLSessionDataTask created internally in the
I have used it and tested it extensively (on live projects) in iOS 10+ and watchOS 3.2+. It probably works in earlier versions but I have not tried it. I suspect it will work just fine on tvOS and macOS but I have not tried that either.
It can be compiled with Swift 3.2 and Swift 4.0 compilers.
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)