Nutan Niraula
Nutan Niraula

Reputation: 156

How can i add authentication header in asnetworkimagenode url request?

I'm using AsyncDisplaykit in my swift application and ASNetworkImageNode as my imageview in collectionNode. I can load any external url with great performance but for my application I need to communicate with our api which requires authentication header to be sent on each GET request. How can i add authentication header in asnetworkimagenode url request or write an extension or any other workaround to achieve this?

Upvotes: 0

Views: 394

Answers (1)

Nutan Niraula
Nutan Niraula

Reputation: 156

I searched the library files and found that there is a setSharedImageManagerWith(_:URLSessionConfiguration?) in PINRemoteImageManager. One can add additional header in session configuration. So in swift 3 the code can be added in appdelegate didFinishLaunchingWithOptions as:

let config = URLSessionConfiguration.ephemeral

    config.httpAdditionalHeaders = [
        "clientid": "yourAdditionalHeader",
        "clientkey": "yourAdditionalHeader"
        ] as [AnyHashable:Any]

    ASPINRemoteImageDownloader.setSharedImageManagerWith(config)

Now setting the url in AsNetworkImageNode will send the url request with additional headers added to the request. This has solved my issue.

The doc of PINRemoteImageManager reads

"Sets the shared instance of PINRemoteImageManager to an instance with the supplied configuration. If configuration is nil, [NSURLSessionConfiguration ephemeralSessionConfiguration] is used. You specify a custom configuration if you need to configure timeout values, cookie policies, additional HTTP headers, etc. This method should not be used if the shared instance has already been created."

So the similar code can be used to configure timeout values, cookie policy and of course additional http headers. Hope this will help someone.

Upvotes: 1

Related Questions