2017-07-19 114 views
1

我正在使用UIImageView.af_setImage扩展从需要认证的webdav服务器下载图像。AlamofireImage addAuthentication not added授权标头

在此基础上说的文档......

如果图像需要从UIImageView的扩展认证证书,它可以提供如下:

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

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

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

and when没有工作

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

但是,似乎都没有发送授权头到HTTP请求。

我错过了什么?

回答

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" 
     ) 
    }() 
}