2017-07-19 73 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" 
     ) 
    }() 
}