0
2

More than 3 years have passed since last update.

Moyaから自己署名証明書を使っているHTTPSサーバーと通信する方法

Posted at

以下の記事を参考に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

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2