如何在asnetworkimagenode url请求中添加身份验证标头?

时间:2022-06-01 20:35:37

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?

我在swift应用程序中使用AsyncDisplaykit,在collectionNode中使用ASNetworkImageNode作为我的imageview。我可以加载任何具有出色性能的外部URL但是对于我的应用程序,我需要与我们的api通信,这需要在每个GET请求上发送身份验证标头。如何在asnetworkimagenode url请求中添加身份验证标头或编写扩展或任何其他解决方法来实现此目的?

1 个解决方案

#1


1  

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:

我搜索了库文件,发现在PINRemoteImageManager中有一个setSharedImageManagerWith(_:URLSessionConfiguration?)。可以在会话配置中添加其他标头。所以在swift 3中,代码可以添加到appdelegate didFinishLaunchingWithOptions中:

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.

现在,在AsNetworkImageNode中设置url将发送url请求,并在请求中添加其他标头。这解决了我的问题。

The doc of PINRemoteImageManager reads

PINRemoteImageManager的文档读取

"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."

“将PINRemoteImageManager的共享实例设置为具有所提供配置的实例。如果配​​置为nil,则使用[NSURLSessionConfiguration ephemeralSessionConfiguration]。如果需要配置超时值,cookie策略,其他HTTP头等,则指定自定义配置。如果已经创建了共享实例,则不应使用该方法。“

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

因此,类似的代码可用于配置超时值,cookie策略以及其他http标头。希望这会对某人有所帮助。

#1


1  

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:

我搜索了库文件,发现在PINRemoteImageManager中有一个setSharedImageManagerWith(_:URLSessionConfiguration?)。可以在会话配置中添加其他标头。所以在swift 3中,代码可以添加到appdelegate didFinishLaunchingWithOptions中:

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.

现在,在AsNetworkImageNode中设置url将发送url请求,并在请求中添加其他标头。这解决了我的问题。

The doc of PINRemoteImageManager reads

PINRemoteImageManager的文档读取

"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."

“将PINRemoteImageManager的共享实例设置为具有所提供配置的实例。如果配​​置为nil,则使用[NSURLSessionConfiguration ephemeralSessionConfiguration]。如果需要配置超时值,cookie策略,其他HTTP头等,则指定自定义配置。如果已经创建了共享实例,则不应使用该方法。“

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

因此,类似的代码可用于配置超时值,cookie策略以及其他http标头。希望这会对某人有所帮助。