以下の記事を参考にMoyaをどのようにカスタムするか
MoyaのSessionDelegateを継承したクラスを作る
class SelfSignedSSLSessionDelegate: SessionDelegate {
func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
let protectionSpace = challenge.protectionSpace
// 認証チャレンジタイプがサーバ認証かどうか確認
// 通信対象のホストは想定しているものかどうか確認
guard protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust,
let serverTrust = protectionSpace.serverTrust
else {
// 特別に検証する対象ではない場合はデフォルトのハンドリングを行う
completionHandler(.performDefaultHandling, nil)
return
}
completionHandler(.useCredential, URLCredential(trust: serverTrust))
}
}
public extension MoyaProvider {
final class func selfSignedSSLAlamofireSession() -> Session {
let configuration = URLSessionConfiguration.default
configuration.headers = .default
return Session(configuration: configuration, delegate: PassmarketSessionDelegate(), startRequestsImmediately: false)
}
}
Moyaを使ったAPIの叩き方
let session = MoyaProvider<hogehogeAPIClient>.selfSignedSSLAlamofireSession()
let provider = MoyaProvider<hogehogeAPIClient>(session: session)
provider.rx.request(.getHogehoge)
.filterSuccessfulStatusCodes()
.subscribe()
.disposed(by: bag)
参考
iOSアプリから自己署名証明書を使っているHTTPSサーバーと通信する
https://qiita.com/chocoyama/items/890e3c35736c74733c55