1

我正在使用 UIImageView.af_setImage 扩展从需要身份验证的 webdav 服务器下载图像。

根据文档说...

如果图像需要来自 UIImageView 扩展的身份验证凭据,则可以按如下方式提供:

ImageDownloader.default.addAuthentication(user: "user", password: "password")

...我尝试以 2 种不同的方式添加身份验证:

ImageDownloader.default.addAuthentication(user: "user", password: "password")

当那不起作用时

UIImageView.af_sharedImageDownloader.addAuthentication(user: "user", password: "password")

但是,似乎都没有将 Authorization 标头发送到 HTTP 请求。

我错过了什么?

4

1 回答 1

0

我没有使用addAuthentication(),而是通过安装我自己UIImageView.af_sharedImageDownloader的添加Authorization: Bearer yZyq6jYn78Ia9EOysUV8kQ标题来解决它。

迅速

import Alamofire
import OAuthSwiftAlamofire
import OAuthSwift
import AlamofireImage

class MyNetwork {
    lazy var sessionManagerForImageDownloader: SessionManager = {
        let configuration: URLSessionConfiguration = ImageDownloader.defaultURLSessionConfiguration()
        let sessionManager = Alamofire.SessionManager(configuration: configuration)
        sessionManager.adapter = self.oauth2Swift.requestAdapter
        sessionManager.retrier = self.oauth2Swift.requestAdapter
        sessionManager.startRequestsImmediately = false
        return sessionManager
    }()

    func setup() {
        // Glue AlamofireImage with OAuthSwift
        // When `af_setImage()` is invoked, then the it will inserts
        // the header field "Authorization: Bearer yZyq6jYn78Ia9EOysUV8kQ"
        UIImageView.af_sharedImageDownloader = ImageDownloader(sessionManager: sessionManagerForImageDownloader)
    }

    lazy var oauth2Swift: OAuth2Swift = {
        return OAuth2Swift(
            consumerKey:    "clientID",
            consumerSecret: "clientSecret",
            authorizeUrl:   "https://example.com/login",
            accessTokenUrl: "https://example.com/login",
            responseType:   "code"
        )
    }()
}
于 2017-12-10T14:55:52.610 回答