Sajid Zeb
Sajid Zeb

Reputation: 1968

Apple's URLSession vs Cronet to support HTTP/3 using QUIC protocols in Swift iOS App

I want to implement my API's hitting mechanism to use HTTP2/3 using QUIC as their transport layer, which provides benefits for reduce latency and improved multiplexing.

I am using Cronet in android native app using java. Same i want to use in my iOS native app using Swift.

Cronet's usage in swift app is not explained anywhere in web.

I am using this library Cronet.framework

and its implementation i found is this.

Add in Podfile and do pod install in terminal.

pod 'Cronet'

then in AppDelegate

import Cronet

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        setupCronet()
        return true
    }

    private func setupCronet() {
        Cronet.setHttp2Enabled(true)
        Cronet.setQuicEnabled(true)
        Cronet.setBrotliEnabled(true)
        Cronet.setHttpCacheType(.memory)
        Cronet.start()
        Cronet.registerHttpProtocolHandler()
    }
}

But there is no other implementation in project to call like in Android we call CronetEngine and then to call the api's.

Question is:

Upvotes: 1

Views: 255

Answers (1)

dgatwood
dgatwood

Reputation: 10407

+1 to the comment advising you to minimize the use of third-party networking libraries.

But to answer the question, my vague recollection is that Cronet installs itself as an NSURLProtocol, so presumably you would need to make sure Cronet is part of the set of protocol classes on your session.

A quick search found this:

https://medium.com/the-react-native-log/using-cronet-in-your-mobile-app-7dda3a89c132

which agrees with that recollection, and provides a useful snippet:

#include <Cronet/Cronet.h>
...
[Cronet setHttp2Enabled:YES];
[Cronet setQuicEnabled:YES];
[Cronet setBrotliEnabled:YES];
[Cronet setHttpCacheType:CRNHttpCacheTypeDisk];
[Cronet addQuicHint:@"yourhostname.tld" port:443 altPort:443];
[Cronet start];
[Cronet registerHttpProtocolHandler];

Note, however, that AFAIK, the registerHttpProtocolHandler method in that snippet registers Cronet only for the shared session. For all other sessions, you'll have to manually add the NSURLProtocol class to the array of protocols registered for that session.

Upvotes: 0

Related Questions