gpt4 book ai didi

ios - 如何在 asnetworkimagenode url 请求中添加身份验证 header ?

转载 作者:行者123 更新时间:2023-11-28 15:24:42 24 4
gpt4 key购买 nike

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

最佳答案

我搜索了库文件,发现PINRemoteImageManager中有一个setSharedImageManagerWith(_:URLSessionConfiguration?)。可以在 session 配置中添加额外的 header 。所以在 swift 3 中,代码可以添加到 appdelegate didFinishLaunchingWithOptions 中:

let config = URLSessionConfiguration.ephemeral

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

ASPINRemoteImageDownloader.setSharedImageManagerWith(config)

现在在 AsNetworkImageNode 中设置 url 将发送带有附加 header 的 url 请求。这解决了我的问题。

PINRemoteImageManager 的文档是这样写的

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

因此类似的代码可用于配置超时值、cookie 策略,当然还有额外的 http header 。希望这会对某人有所帮助。

关于ios - 如何在 asnetworkimagenode url 请求中添加身份验证 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45430972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com